All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.msofficexml.docx.wordprocessingml.WmlBody Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml.msofficexml.docx.wordprocessingml;


import com.hfg.graphics.units.Points;
import com.hfg.util.StringUtil;
import com.hfg.xml.msofficexml.docx.Docx;
import com.hfg.xml.msofficexml.docx.part.DocumentPart;
import com.hfg.xml.msofficexml.docx.part.WmlVbaPart;
import com.hfg.xml.msofficexml.docx.wordprocessingml.field.WmlComplexFieldCharType;
import com.hfg.xml.msofficexml.docx.wordprocessingml.style.WmlParagraphStyle;

//------------------------------------------------------------------------------
/**
 Represents an Office Open XML body (<w:body>) tag.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class WmlBody extends WmlContentContainer
{
   private WmlSectionProperties   mSectionProperties;

   private static final String DEFAULT_TOC_INSTRUCTIONS = " TOC \\o \"1-3\" \\h \\z \\u ";

   //###########################################################################
   // CONSTRUCTORS
   //###########################################################################

   //---------------------------------------------------------------------------
   public WmlBody(Docx inDocx)
   {
      super(WmlXML.BODY, inDocx);

      setPart(inDocx.getDocumentPart());
   }


   //###########################################################################
   // PUBLIC METHODS
   //###########################################################################

   //---------------------------------------------------------------------------
   @Override
   public DocumentPart getPart()
   {
      return (DocumentPart) super.getPart();
   }

   //---------------------------------------------------------------------------
   public WmlParagraph addParagraph()
   {
      WmlParagraph p = new WmlParagraph(getParentDoc());
      addSubtag(p);

      return p;
   }

   //---------------------------------------------------------------------------
   public WmlParagraph addParagraph(String inContent)
   {
      WmlParagraph p = addParagraph();
      p.addTextRun().addText(inContent);

      return p;
   }

   //---------------------------------------------------------------------------
   public void addParagraph(WmlParagraph inContent)
   {
      addSubtag(inContent);
      inContent.setParentDoc(getParentDoc());
   }

   //---------------------------------------------------------------------------
   public void addSubDoc(Docx inValue)
   {
      addSubtag(new WmlSubDoc(getParentDoc()).setDocument(inValue));
   }

   //---------------------------------------------------------------------------
   public WmlTable addTable()
   {
      WmlTable table = new WmlTable(getParentDoc());
      addSubtag(table);

      return table;
   }

   //---------------------------------------------------------------------------
   public WmlUnorderedList addUnorderedList()
   {
      WmlUnorderedList unorderedList = new WmlUnorderedList(getParentDoc());
      addSubtag(unorderedList);

      return unorderedList;
   }

   //---------------------------------------------------------------------------
   public WmlOrderedList addOrderedList()
   {
      WmlOrderedList list = new WmlOrderedList(getParentDoc());
      addSubtag(list);

      return list;
   }

   //---------------------------------------------------------------------------
   public WmlBody addTOC()
   {
      return addTOC(null);
   }

   //---------------------------------------------------------------------------
   public WmlBody addTOC(String inInstructionText)
   {
      WmlStructuredDocumentTag sdt = new WmlStructuredDocumentTag(getParentDoc());
      addSubtag(sdt);

      sdt.getProperties().setAsTOC();
      WmlTextRunProperties runProperties = sdt.getEndProperties().getTextRunProperties();
      runProperties.setSize(new Points(12))
                   .setComplexScriptSize(new Points(12));

      WmlStructuredDocumentTagContent sdtContent = sdt.getContentTag();

      WmlParagraph p = sdtContent.addParagraph();
      p.getProperties().setStyle(WmlParagraphStyle.TOC_HEADING_STYLE_ID);
      p.addTextRun("Table of Contents");


      p = sdtContent.addParagraph();

      WmlTab tab = new WmlTab(getParentDoc())
            .setTabStyle(WmlTabStyle.END)
            .setPosition(new Points(9350 / 20))  // dxa = twentieth of a point
            .setTabLeader(WmlTabLeader.DOT);
      p.getProperties().setStyle(WmlParagraphStyle.TOC1_STYLE_ID).addTab(tab);

      // Begin
      p.addTextRun().addComplexField(WmlComplexFieldCharType.begin);

      // Instruction
      p.addTextRun().addInstructionText(StringUtil.isSet(inInstructionText) ? inInstructionText : DEFAULT_TOC_INSTRUCTIONS);

      // Separate
      p.addTextRun().addComplexField(WmlComplexFieldCharType.separate);

      // End
      p.addTextRun().addComplexField(WmlComplexFieldCharType.end);

      // This next bit is to get the TOC to build when the user opens the document.

      // Add the macro which will AutoOpen (rebuild the TOC)
      getParentDoc().addVBA_Part(new WmlVbaPart(WmlVbaPart.getVBA_ProjectStream(), WmlVbaPart.VBA_PROJECT_NAME));
      getParentDoc().getVBA_DataPart().addMacro(WmlVbaPart.AUTO_OPEN_VBA);

      return this;
   }

   //---------------------------------------------------------------------------
   public WmlSectionProperties getSectionProperties()
   {
      if (null == mSectionProperties)
      {
         // Check it it has been added via addSubtag()...
         mSectionProperties = getOptionalSubtagByName(WmlXML.SECT_PROPS);
         if (null == mSectionProperties)
         {
            mSectionProperties = new WmlSectionProperties(getParentDoc());
            addSubtag(mSectionProperties);
         }
      }

      return mSectionProperties;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy