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

com.hfg.xml.msofficexml.docx.Docx Maven / Gradle / Ivy

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


import java.awt.Font;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfg.util.StringUtil;
import com.hfg.xml.msofficexml.OfficeOpenXMLContentType;
import com.hfg.xml.msofficexml.OfficeOpenXmlDocument;
import com.hfg.xml.msofficexml.RelationshipType;
import com.hfg.xml.msofficexml.docx.part.*;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlBookmark;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlFooter;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlHeader;
import com.hfg.xml.msofficexml.docx.wordprocessingml.WmlXML;
import com.hfg.xml.msofficexml.part.*;


//------------------------------------------------------------------------------
/**
 Office Open XML format Word document.

 @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]
//------------------------------------------------------------------------------
/*

 Testing notes:

 -  for zipping on the command-line:  'zip -r  *'

 -  docProps/app.xml is not required as long as there is no rel entry
 -  docProps/thumbnail.jpeg is not required as long as there is no rel entry
 */
public class Docx extends OfficeOpenXmlDocument
{
   private DocumentPart                mDocumentPart;
   private MainDocPartRelationshipPart mMainDocPartRelationshipPart;
   private CommentsPart                mCommentsPart;
   private StylesPart                  mStylesPart;
   private SettingsPart                mSettingsPart;
   private List            mHeaderParts;
   private List            mFooterParts;

   // For convenience, keep a list of bookmarks in the document.
   private Map    mBookmarkMap;

   private static String sDefaultName = "Untitled.docx";

    //---------------------------------------------------------------------------
   public Docx()
   {
      super();

      init();
   }

   //---------------------------------------------------------------------------
   private void init()
   {
      // Setup essential relationships
      getPackageRelationshipPart().addRelationship(RelationshipType.OFFICE_DOCUMENT, WmlXML.DOCUMENT_FILE);

      mDocumentPart = new DocumentPart(this);

      getContentTypesPart().addDefault(WmlContentType.PRINTER_SETTINGS);
      getContentTypesPart().addOverride(mDocumentPart, WmlContentType.MAIN_DOCUMENT);
 /*
      getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/docProps/app.xml")), OfficeOpenXMLContentType.EXTENDED_PROPERTIES);
      getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/word/footnotes.xml")), WmlContentType.FOOTNOTES);
      getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/word/fontTable.xml")), WmlContentType.FONT_TABLE);
      getContentTypesPart().addOverride(new OfficeXMLPart(this).setFile(new File("/word/webSettings.xml")), WmlContentType.WEB_SETTINGS);
*/
   }


   //---------------------------------------------------------------------------
   public static String getDefaultName()
   {
      return sDefaultName;
   }

   //---------------------------------------------------------------------------
   @Override
   public String name()
   {
      String name = super.name();
      if (! StringUtil.isSet(name))
      {
         name = getDefaultName();
         setName(name);
      }

      return name;
   }

   //---------------------------------------------------------------------------
   /**
    Convenience method for setting the default font for the document.
    * @param inValue Font object to use as the default font-family, size, and style.
    * @return the doument object to enable method chaining.
    */
   public Docx setDefaultFont(Font inValue)
   {
      getStylesPart().getDocDefaults().getTextRunProperties().setFont(inValue);
      return this;
   }

   //---------------------------------------------------------------------------
   public MainDocPartRelationshipPart getMainDocPartRelationshipPart()
   {
      if (null == mMainDocPartRelationshipPart)
      {
         mMainDocPartRelationshipPart = new MainDocPartRelationshipPart(this);
         addPart(mMainDocPartRelationshipPart);
      }

      return mMainDocPartRelationshipPart;
   }

   //---------------------------------------------------------------------------
   public SettingsPart getSettingsPart()
   {
      if (null == mSettingsPart)
      {
         mSettingsPart = new SettingsPart(this);
         addPart(mSettingsPart);
     
         getContentTypesPart().addOverride(mSettingsPart, WmlContentType.SETTINGS);
      }

      return mSettingsPart;
   }

   //---------------------------------------------------------------------------
   public DocumentPart getDocumentPart()
   {
      return mDocumentPart;
   }

   //---------------------------------------------------------------------------
   public StylesPart getStylesPart()
   {
      if (null == mStylesPart)
      {
         mStylesPart = new StylesPart(this);
         addPart(mStylesPart);

         getMainDocPartRelationshipPart().addRelationship(RelationshipType.STYLES, WmlXML.STYLES_FILE);
         getContentTypesPart().addOverride(mStylesPart, WmlContentType.STYLES);
      }

      return mStylesPart;
   }

   //---------------------------------------------------------------------------
   // The comments part will only be instantiated if requested.
   public CommentsPart getCommentsPart()
   {
      if (null == mCommentsPart)
      {
         mCommentsPart = new CommentsPart(this);
         mCommentsPart.setParentDoc(this);
         addPart(mCommentsPart);
         getPackageRelationshipPart().addRelationship(RelationshipType.COMMENTS, WmlXML.COMMENTS_FILE);
         getContentTypesPart().addOverride(mCommentsPart,    OfficeOpenXMLContentType.COMMENTS);
      }

      return mCommentsPart;
   }

   //---------------------------------------------------------------------------
   public String addHeader(WmlHeader inValue)
   {
      HeaderPart headerPart = new HeaderPart(this, inValue).setIndex(mHeaderParts != null ? mHeaderParts.size() + 1 : 1);
      if (null == mHeaderParts)
      {
         mHeaderParts = new ArrayList<>(5);
      }

      mHeaderParts.add(headerPart);
      addPart(headerPart);
      getContentTypesPart().addOverride(headerPart, OfficeOpenXMLContentType.HEADER);

      return getMainDocPartRelationshipPart().addRelationship(RelationshipType.HEADER, headerPart.getFile());
   }

   //---------------------------------------------------------------------------
   public String addFooter(WmlFooter inValue)
   {
      FooterPart footerPart = new FooterPart(this, inValue).setIndex(mFooterParts != null ? mFooterParts.size() + 1 : 1);
      if (null == mFooterParts)
      {
         mFooterParts = new ArrayList<>(5);
      }

      mFooterParts.add(footerPart);
      addPart(footerPart);
      getContentTypesPart().addOverride(footerPart, OfficeOpenXMLContentType.FOOTER);

      return getMainDocPartRelationshipPart().addRelationship(RelationshipType.FOOTER, footerPart.getFile());
   }

   //---------------------------------------------------------------------------
   /**
    For convenience, bookmarks are automatically registered via this method so that
    they can be retrieved via the getBookmark() method.
    * @param inValue
    */
   public void registerBookmark(WmlBookmark inValue)
   {
      if (null == mBookmarkMap)
      {
         mBookmarkMap = new HashMap<>(50);
      }

      mBookmarkMap.put(inValue.name(), inValue);
   }

   //---------------------------------------------------------------------------
   public WmlBookmark getBookmark(String inName)
   {
      WmlBookmark bookmark = null;
      if (mBookmarkMap != null)
      {
         bookmark = mBookmarkMap.get(inName);
      }

      return bookmark;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy