Package org.docx4j

Class Docx4J

java.lang.Object
org.docx4j.Docx4J

public class Docx4J
extends java.lang.Object
This is a facade for some typical uses of Docx4J:
  • Loading a document
  • Saving a document
  • Binding xml to content controls in a document
  • Exporting the document (to HTML, or PDF and other formats supported by the FO renderer)
  • Field Details

    • MIME_PDF

      public static final java.lang.String MIME_PDF
      See Also:
      Constant Field Values
    • MIME_FO

      public static final java.lang.String MIME_FO
      See Also:
      Constant Field Values
    • FLAG_NONE

      public static final int FLAG_NONE
      No flags passed, do the default
      See Also:
      Constant Field Values
    • FLAG_EXPORT_PREFER_XSL

      public static final int FLAG_EXPORT_PREFER_XSL
      If available export the document using a xsl transformation
      See Also:
      Constant Field Values
    • FLAG_EXPORT_PREFER_NONXSL

      public static final int FLAG_EXPORT_PREFER_NONXSL
      If available export the document using a visitor
      See Also:
      Constant Field Values
    • FLAG_SAVE_ZIP_FILE

      public static final int FLAG_SAVE_ZIP_FILE
      Save the document in a zip container (default docx)
      See Also:
      Constant Field Values
    • FLAG_SAVE_FLAT_XML

      public static final int FLAG_SAVE_FLAT_XML
      Save the document as a flat xml document
      See Also:
      Constant Field Values
    • FLAG_SAVE_ENCRYPTED_BINARYRC4

      public static final int FLAG_SAVE_ENCRYPTED_BINARYRC4
      RC4 is weak, so don't use it unless you have to for backwards compatibility purposes (ie the applications to be used for reading your docx don't support anything better). See further http://blogs.msdn.com/b/david_leblanc/archive/2010/04/16/don-t-use-office-rc4-encryption-really-just-don-t-do-it.aspx
      See Also:
      Constant Field Values
    • FLAG_SAVE_ENCRYPTED_STANDARD

      public static final int FLAG_SAVE_ENCRYPTED_STANDARD
      Standard encryption: This approach uses a binary EncryptionInfo structure. It uses Advanced Encryption Standard (AES) as an encryption algorithm and SHA-1 as a hashing algorithm.
      See Also:
      Constant Field Values
    • FLAG_SAVE_ENCRYPTED_AGILE

      public static final int FLAG_SAVE_ENCRYPTED_AGILE
      Agile encryption: This is used by Word 2010, it uses an XML EncryptionInfo structure. The encryption and hashing algorithms are specified in the structure and can be for any encryption supported on the host computer.
      See Also:
      Constant Field Values
    • FLAG_BIND_INSERT_XML

      public static final int FLAG_BIND_INSERT_XML
      inject the passed xml into the document if you don't do this step, then the xml in the document will be used.
      See Also:
      Constant Field Values
    • FLAG_BIND_BIND_XML

      public static final int FLAG_BIND_BIND_XML
      Insert the data of the xml in the content controls Not needed, if the document will only be opened in Word and not converted to other formats.
      See Also:
      Constant Field Values
    • FLAG_BIND_REMOVE_SDT

      public static final int FLAG_BIND_REMOVE_SDT
      Remove the content controls of the document
      See Also:
      Constant Field Values
    • FLAG_BIND_REMOVE_XML

      public static final int FLAG_BIND_REMOVE_XML
      Remove any xml parts from the document that are used with the content controls.
      See Also:
      Constant Field Values
    • NS_CONDITIONS

      protected static final java.lang.String NS_CONDITIONS
      See Also:
      Constant Field Values
    • NS_XPATHS

      protected static final java.lang.String NS_XPATHS
      See Also:
      Constant Field Values
    • NS_QUESTIONS

      protected static final java.lang.String NS_QUESTIONS
      See Also:
      Constant Field Values
    • NS_COMPONENTS

      protected static final java.lang.String NS_COMPONENTS
      See Also:
      Constant Field Values
    • PART_TO_REMOVE_SCHEMA_TYPES

      protected static final java.util.Set<java.lang.String> PART_TO_REMOVE_SCHEMA_TYPES
    • EXPORT_FO_DETECTED

      public static java.lang.Boolean EXPORT_FO_DETECTED
    • EXPORT_DOCUMENTS4J_REMOTE_DETECTED

      public static java.lang.Boolean EXPORT_DOCUMENTS4J_REMOTE_DETECTED
    • EXPORT_DOCUMENTS4J_LOCAL_DETECTED

      public static java.lang.Boolean EXPORT_DOCUMENTS4J_LOCAL_DETECTED
    • EXPORT_MICROSOFT_GRAPH_DETECTED

      public static java.lang.Boolean EXPORT_MICROSOFT_GRAPH_DETECTED
    • EXPORT_LEGACY_CONVERTER_DETECTED

      public static java.lang.Boolean EXPORT_LEGACY_CONVERTER_DETECTED
  • Constructor Details

    • Docx4J

      public Docx4J()
  • Method Details

    • setEventNotifier

      public static void setEventNotifier​(net.engio.mbassy.bus.MBassador<Docx4jEvent> eventbus)
    • load

      public static OpcPackage load​(java.io.File inFile, Filetype type) throws Docx4JException
      Load a docx/pptx/xlsx from a File, where you know in advance what type (zip, compound, flat opc) it is. Typically you'll cast the result to WordprocessingMLPackage or pptx/xlsx equivalent.
      Throws:
      Docx4JException
      Since:
      6.0.0
    • load

      public static WordprocessingMLPackage load​(java.io.File inFile) throws Docx4JException
      Load a Docx Document from a File
      Throws:
      Docx4JException
    • load

      public static WordprocessingMLPackage load​(PackageIdentifier pkgIdentifier, java.io.File inFile) throws Docx4JException
      Load a Docx Document from a File, assigning it an identifier for eventing
      Throws:
      Docx4JException
      Since:
      3.1.0
    • load

      public static WordprocessingMLPackage load​(java.io.InputStream inStream) throws Docx4JException
      Load a Docx Document from an InputStream
      Throws:
      Docx4JException
    • load

      public static WordprocessingMLPackage load​(PackageIdentifier pkgIdentifier, java.io.InputStream inStream) throws Docx4JException
      Load a Docx Document from an InputStream, assigning it an identifier for eventing
      Throws:
      Docx4JException
      Since:
      3.1.0
    • save

      public static void save​(OpcPackage pkg, java.io.File outFile) throws Docx4JException
      Save a Ddocx/pptx/xlsx to a File.
      Throws:
      Docx4JException
      Since:
      3.3.0
    • save

      public static void save​(OpcPackage pkg, java.io.File outFile, int flags) throws Docx4JException
      Save a docx/pptx/xlsx to a File. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML
      Throws:
      Docx4JException
    • save

      public static void save​(OpcPackage pkg, java.io.OutputStream outStream) throws Docx4JException
      Save a docx/pptx/xlsx to an OutputStream using flag Docx4J.FLAG_SAVE_ZIP_FILE
      Throws:
      Docx4JException
      Since:
      3.3.0
    • save

      public static void save​(OpcPackage pkg, java.io.OutputStream outStream, int flags) throws Docx4JException
      Save a docx/pptx/xlsx to an OutputStream. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML
      Throws:
      Docx4JException
    • save

      public static void save​(OpcPackage pkg, java.io.File outFile, int flags, java.lang.String password) throws Docx4JException
      Save a docx/pptx/xlsx to a File. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML or one of the Docx4J.FLAG_SAVE_ENCRYPTED_ variants (recommend FLAG_SAVE_ENCRYPTED_AGILE) For the FLAG_SAVE_ENCRYPTED_ variants, you need to provide a password.
      Throws:
      Docx4JException
    • save

      public static void save​(OpcPackage pkg, java.io.OutputStream outStream, int flags, java.lang.String password) throws Docx4JException
      Save this pkg to an OutputStream. The flag is typically Docx4J.FLAG_SAVE_ZIP_FILE or Docx4J.FLAG_SAVE_FLAT_XML or one of the Docx4J.FLAG_SAVE_ENCRYPTED_ variants (recommend FLAG_SAVE_ENCRYPTED_AGILE) For the FLAG_SAVE_ENCRYPTED_ variants, you need to provide a password.
      Throws:
      Docx4JException
    • bind

      public static void bind​(WordprocessingMLPackage wmlPackage, java.lang.String xmlDocument, int flags) throws Docx4JException
      Bind the content controls of the passed document to the xml.
      Throws:
      Docx4JException
    • bind

      public static void bind​(WordprocessingMLPackage wmlPackage, java.io.InputStream xmlDocument, int flags) throws Docx4JException
      Bind the content controls of the passed document to the xml.
      Throws:
      Docx4JException
    • bind

      public static void bind​(WordprocessingMLPackage wmlPackage, Answers answers, int flags) throws Docx4JException
      Bind the content controls of the passed document to the xml (here using optional standardised Answers format).
      Throws:
      Docx4JException
    • bind

      public static void bind​(WordprocessingMLPackage wmlPackage, org.w3c.dom.Document xmlDocument, int flags) throws Docx4JException
      Bind the content controls of the passed document to the xml.
      Throws:
      Docx4JException
    • bind

      public static void bind​(WordprocessingMLPackage wmlPackage, org.w3c.dom.Document xmlDocument, int flags, DocxFetcher docxFetcher) throws Docx4JException
      Bind the content controls of the passed document to the xml.
      Throws:
      Docx4JException
    • bind

      public static void bind​(WordprocessingMLPackage wmlPackage, org.w3c.dom.Document xmlDocument, int flags, DocxFetcher docxFetcher, XsltProvider xsltProvider, java.lang.String xsltFinisherfilename, java.util.Map<java.lang.String,​java.util.Map<java.lang.String,​java.lang.Object>> finisherParams) throws Docx4JException
      Bind the content controls of the passed document to the xml, applying some formatting finishing touches to the final docx.
      Throws:
      Docx4JException
      Since:
      6.1.0
    • insertXMLData

      protected static void insertXMLData​(CustomXmlPart customXmlDataStoragePart, org.w3c.dom.Document xmlDocument) throws Docx4JException
      Throws:
      Docx4JException
    • findXPathStorageItemIdInContentControls

      protected static java.lang.String findXPathStorageItemIdInContentControls​(WordprocessingMLPackage wmlPackage)
    • removeSDTs

      protected static void removeSDTs​(WordprocessingMLPackage wmlPackage) throws Docx4JException
      Throws:
      Docx4JException
    • removeDefinedCustomXmlParts

      protected static void removeDefinedCustomXmlParts​(WordprocessingMLPackage wmlPackage, java.lang.String itemId)
    • clone

      public static WordprocessingMLPackage clone​(WordprocessingMLPackage wmlPackage) throws Docx4JException
      Duplicate the document
      Throws:
      Docx4JException
    • createFOSettings

      public static FOSettings createFOSettings()
      Create the configuration object for conversions that are done via xsl-fo
    • toFO

      public static void toFO​(FOSettings settings, java.io.OutputStream outputStream, int flags) throws Docx4JException
      Convert the document via xsl-fo
      Throws:
      Docx4JException
    • toPDF

      public static void toPDF​(WordprocessingMLPackage wmlPackage, java.io.OutputStream outputStream) throws Docx4JException
      Convenience method to convert the document to PDF
      Throws:
      Docx4JException
    • pdfViaLegacyConverter

      public static boolean pdfViaLegacyConverter()
      If the docx4j-legacy-converter project is present, we'll use it for PDF export.
      Returns:
      Since:
      8.3.1
    • pdfViaFO

      public static boolean pdfViaFO()
      If the docx4j-export-fo project is present, we'll use FO for PDF export.
      Returns:
      Since:
      3.3.0
    • pdfViaDocuments4jRemote

      public static boolean pdfViaDocuments4jRemote()
      Since:
      8.2.0
    • pdfViaDocuments4jLocal

      public static boolean pdfViaDocuments4jLocal()
      Since:
      8.2.0
    • pdfViaMicrosoftGraph

      public static boolean pdfViaMicrosoftGraph()
      Since:
      8.2.7
    • getFOExporter

      protected static Exporter<FOSettings> getFOExporter​(int flags) throws Docx4JException
      Throws:
      Docx4JException
    • createHTMLSettings

      public static HTMLSettings createHTMLSettings()
      Create the configuration object for conversions to html
    • toHTML

      public static void toHTML​(HTMLSettings settings, java.io.OutputStream outputStream, int flags) throws Docx4JException
      Convert the document to HTML
      Throws:
      Docx4JException
    • toHTML

      public static void toHTML​(WordprocessingMLPackage wmlPackage, java.lang.String imageDirPath, java.lang.String imageTargetUri, java.io.OutputStream outputStream) throws Docx4JException
      Convert the document to HTML
      Throws:
      Docx4JException
    • getHTMLExporter

      protected static Exporter<HTMLSettings> getHTMLExporter​(int flags)