001package com.hfg.xml.msofficexml.docx.wordprocessingml; 002 003 004import com.hfg.graphics.units.Points; 005import com.hfg.util.StringUtil; 006import com.hfg.xml.msofficexml.docx.Docx; 007import com.hfg.xml.msofficexml.docx.part.DocumentPart; 008import com.hfg.xml.msofficexml.docx.part.WmlVbaPart; 009import com.hfg.xml.msofficexml.docx.wordprocessingml.field.WmlComplexFieldCharType; 010import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlParagraphStyle; 011 012//------------------------------------------------------------------------------ 013/** 014 Represents an Office Open XML body (<w:body>) tag. 015 016 @author J. Alex Taylor, hairyfatguy.com 017 */ 018//------------------------------------------------------------------------------ 019// com.hfg XML/HTML Coding Library 020// 021// This library is free software; you can redistribute it and/or 022// modify it under the terms of the GNU Lesser General Public 023// License as published by the Free Software Foundation; either 024// version 2.1 of the License, or (at your option) any later version. 025// 026// This library is distributed in the hope that it will be useful, 027// but WITHOUT ANY WARRANTY; without even the implied warranty of 028// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 029// Lesser General Public License for more details. 030// 031// You should have received a copy of the GNU Lesser General Public 032// License along with this library; if not, write to the Free Software 033// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 034// 035// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 036// jataylor@hairyfatguy.com 037//------------------------------------------------------------------------------ 038 039public class WmlBody extends WmlContentContainer 040{ 041 private WmlSectionProperties mSectionProperties; 042 043 private static final String DEFAULT_TOC_INSTRUCTIONS = " TOC \\o \"1-3\" \\h \\z \\u "; 044 045 //########################################################################### 046 // CONSTRUCTORS 047 //########################################################################### 048 049 //--------------------------------------------------------------------------- 050 public WmlBody(Docx inDocx) 051 { 052 super(WmlXML.BODY, inDocx); 053 054 setPart(inDocx.getDocumentPart()); 055 } 056 057 058 //########################################################################### 059 // PUBLIC METHODS 060 //########################################################################### 061 062 //--------------------------------------------------------------------------- 063 @Override 064 public DocumentPart getPart() 065 { 066 return (DocumentPart) super.getPart(); 067 } 068 069 //--------------------------------------------------------------------------- 070 public WmlParagraph addParagraph() 071 { 072 WmlParagraph p = new WmlParagraph(getParentDoc()); 073 addSubtag(p); 074 075 return p; 076 } 077 078 //--------------------------------------------------------------------------- 079 public WmlParagraph addParagraph(String inContent) 080 { 081 WmlParagraph p = addParagraph(); 082 p.addTextRun().addText(inContent); 083 084 return p; 085 } 086 087 //--------------------------------------------------------------------------- 088 public void addParagraph(WmlParagraph inContent) 089 { 090 addSubtag(inContent); 091 inContent.setParentDoc(getParentDoc()); 092 } 093 094 //--------------------------------------------------------------------------- 095 public void addSubDoc(Docx inValue) 096 { 097 addSubtag(new WmlSubDoc(getParentDoc()).setDocument(inValue)); 098 } 099 100 //--------------------------------------------------------------------------- 101 public WmlTable addTable() 102 { 103 WmlTable table = new WmlTable(getParentDoc()); 104 addSubtag(table); 105 106 return table; 107 } 108 109 //--------------------------------------------------------------------------- 110 public WmlUnorderedList addUnorderedList() 111 { 112 WmlUnorderedList unorderedList = new WmlUnorderedList(getParentDoc()); 113 addSubtag(unorderedList); 114 115 return unorderedList; 116 } 117 118 //--------------------------------------------------------------------------- 119 public WmlOrderedList addOrderedList() 120 { 121 WmlOrderedList list = new WmlOrderedList(getParentDoc()); 122 addSubtag(list); 123 124 return list; 125 } 126 127 //--------------------------------------------------------------------------- 128 public WmlBody addTOC() 129 { 130 return addTOC(null); 131 } 132 133 //--------------------------------------------------------------------------- 134 public WmlBody addTOC(String inInstructionText) 135 { 136 WmlStructuredDocumentTag sdt = new WmlStructuredDocumentTag(getParentDoc()); 137 addSubtag(sdt); 138 139 sdt.getProperties().setAsTOC(); 140 WmlTextRunProperties runProperties = sdt.getEndProperties().getTextRunProperties(); 141 runProperties.setSize(new Points(12)) 142 .setComplexScriptSize(new Points(12)); 143 144 WmlStructuredDocumentTagContent sdtContent = sdt.getContentTag(); 145 146 WmlParagraph p = sdtContent.addParagraph(); 147 p.getProperties().setStyle(WmlParagraphStyle.TOC_HEADING_STYLE_ID); 148 p.addTextRun("Table of Contents"); 149 150 151 p = sdtContent.addParagraph(); 152 153 WmlTab tab = new WmlTab(getParentDoc()) 154 .setTabStyle(WmlTabStyle.END) 155 .setPosition(new Points(9350 / 20)) // dxa = twentieth of a point 156 .setTabLeader(WmlTabLeader.DOT); 157 p.getProperties().setStyle(WmlParagraphStyle.TOC1_STYLE_ID).addTab(tab); 158 159 // Begin 160 p.addTextRun().addComplexField(WmlComplexFieldCharType.begin); 161 162 // Instruction 163 p.addTextRun().addInstructionText(StringUtil.isSet(inInstructionText) ? inInstructionText : DEFAULT_TOC_INSTRUCTIONS); 164 165 // Separate 166 p.addTextRun().addComplexField(WmlComplexFieldCharType.separate); 167 168 // End 169 p.addTextRun().addComplexField(WmlComplexFieldCharType.end); 170 171 // This next bit is to get the TOC to build when the user opens the document. 172 173 // Add the macro which will AutoOpen (rebuild the TOC) 174 getParentDoc().addVBA_Part(new WmlVbaPart(WmlVbaPart.getVBA_ProjectStream(), WmlVbaPart.VBA_PROJECT_NAME)); 175 getParentDoc().getVBA_DataPart().addMacro(WmlVbaPart.AUTO_OPEN_VBA); 176 177 return this; 178 } 179 180 //--------------------------------------------------------------------------- 181 public WmlSectionProperties getSectionProperties() 182 { 183 if (null == mSectionProperties) 184 { 185 // Check it it has been added via addSubtag()... 186 mSectionProperties = getOptionalSubtagByName(WmlXML.SECT_PROPS); 187 if (null == mSectionProperties) 188 { 189 mSectionProperties = new WmlSectionProperties(getParentDoc()); 190 addSubtag(mSectionProperties); 191 } 192 } 193 194 return mSectionProperties; 195 } 196 197}