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)
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classDocx4J.FindContentControlsVisitor -
Field Summary
Fields Modifier and Type Field Description static java.lang.BooleanEXPORT_DOCUMENTS4J_LOCAL_DETECTEDstatic java.lang.BooleanEXPORT_DOCUMENTS4J_REMOTE_DETECTEDstatic java.lang.BooleanEXPORT_FO_DETECTEDstatic java.lang.BooleanEXPORT_MICROSOFT_GRAPH_DETECTEDstatic intFLAG_BIND_BIND_XMLInsert 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.static intFLAG_BIND_INSERT_XMLinject the passed xml into the document if you don't do this step, then the xml in the document will be used.static intFLAG_BIND_REMOVE_SDTRemove the content controls of the documentstatic intFLAG_BIND_REMOVE_XMLRemove any xml parts from the document that are used with the content controls.static intFLAG_EXPORT_PREFER_NONXSLIf available export the document using a visitorstatic intFLAG_EXPORT_PREFER_XSLIf available export the document using a xsl transformationstatic intFLAG_NONENo flags passed, do the defaultstatic intFLAG_SAVE_ENCRYPTED_AGILEAgile encryption: This is used by Word 2010, it uses an XML EncryptionInfo structure.static intFLAG_SAVE_ENCRYPTED_BINARYRC4RC4 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).static intFLAG_SAVE_ENCRYPTED_STANDARDStandard encryption: This approach uses a binary EncryptionInfo structure.static intFLAG_SAVE_FLAT_XMLSave the document as a flat xml documentstatic intFLAG_SAVE_ZIP_FILESave the document in a zip container (default docx)static java.lang.StringMIME_FOstatic java.lang.StringMIME_PDFprotected static java.lang.StringNS_COMPONENTSprotected static java.lang.StringNS_CONDITIONSprotected static java.lang.StringNS_QUESTIONSprotected static java.lang.StringNS_XPATHSprotected static java.util.Set<java.lang.String>PART_TO_REMOVE_SCHEMA_TYPES -
Constructor Summary
Constructors Constructor Description Docx4J() -
Method Summary
Modifier and Type Method Description static voidbind(WordprocessingMLPackage wmlPackage, java.io.InputStream xmlDocument, int flags)Bind the content controls of the passed document to the xml.static voidbind(WordprocessingMLPackage wmlPackage, java.lang.String xmlDocument, int flags)Bind the content controls of the passed document to the xml.static voidbind(WordprocessingMLPackage wmlPackage, Answers answers, int flags)Bind the content controls of the passed document to the xml (here using optional standardised Answers format).static voidbind(WordprocessingMLPackage wmlPackage, org.w3c.dom.Document xmlDocument, int flags)Bind the content controls of the passed document to the xml.static voidbind(WordprocessingMLPackage wmlPackage, org.w3c.dom.Document xmlDocument, int flags, DocxFetcher docxFetcher)Bind the content controls of the passed document to the xml.static voidbind(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)Bind the content controls of the passed document to the xml, applying some formatting finishing touches to the final docx.static WordprocessingMLPackageclone(WordprocessingMLPackage wmlPackage)Duplicate the documentstatic FOSettingscreateFOSettings()Create the configuration object for conversions that are done via xsl-fostatic HTMLSettingscreateHTMLSettings()Create the configuration object for conversions to htmlprotected static java.lang.StringfindXPathStorageItemIdInContentControls(WordprocessingMLPackage wmlPackage)protected static Exporter<FOSettings>getFOExporter(int flags)protected static Exporter<HTMLSettings>getHTMLExporter(int flags)protected static voidinsertXMLData(CustomXmlPart customXmlDataStoragePart, org.w3c.dom.Document xmlDocument)static WordprocessingMLPackageload(java.io.File inFile)Load a Docx Document from a Filestatic OpcPackageload(java.io.File inFile, Filetype type)Load a docx/pptx/xlsx from a File, where you know in advance what type (zip, compound, flat opc) it is.static WordprocessingMLPackageload(java.io.InputStream inStream)Load a Docx Document from an InputStreamstatic WordprocessingMLPackageload(PackageIdentifier pkgIdentifier, java.io.File inFile)Load a Docx Document from a File, assigning it an identifier for eventingstatic WordprocessingMLPackageload(PackageIdentifier pkgIdentifier, java.io.InputStream inStream)Load a Docx Document from an InputStream, assigning it an identifier for eventingstatic booleanpdfViaDocuments4jLocal()static booleanpdfViaDocuments4jRemote()static booleanpdfViaFO()If the docx4j-export-fo project is present, we'll use FO for PDF export.static booleanpdfViaMicrosoftGraph()protected static voidremoveDefinedCustomXmlParts(WordprocessingMLPackage wmlPackage, java.lang.String itemId)protected static voidremoveSDTs(WordprocessingMLPackage wmlPackage)static voidsave(OpcPackage pkg, java.io.File outFile)Save a Ddocx/pptx/xlsx to a File.static voidsave(OpcPackage pkg, java.io.File outFile, int flags)Save a docx/pptx/xlsx to a File.static voidsave(OpcPackage pkg, java.io.File outFile, int flags, java.lang.String password)Save a docx/pptx/xlsx to a File.static voidsave(OpcPackage pkg, java.io.OutputStream outStream)Save a docx/pptx/xlsx to an OutputStream using flag Docx4J.FLAG_SAVE_ZIP_FILEstatic voidsave(OpcPackage pkg, java.io.OutputStream outStream, int flags)Save a docx/pptx/xlsx to an OutputStream.static voidsave(OpcPackage pkg, java.io.OutputStream outStream, int flags, java.lang.String password)Save this pkg to an OutputStream.static voidsetEventNotifier(net.engio.mbassy.bus.MBassador<Docx4jEvent> eventbus)static voidtoFO(FOSettings settings, java.io.OutputStream outputStream, int flags)Convert the document via xsl-fostatic voidtoHTML(HTMLSettings settings, java.io.OutputStream outputStream, int flags)Convert the document to HTMLstatic voidtoHTML(WordprocessingMLPackage wmlPackage, java.lang.String imageDirPath, java.lang.String imageTargetUri, java.io.OutputStream outputStream)Convert the document to HTMLstatic voidtoPDF(WordprocessingMLPackage wmlPackage, java.io.OutputStream outputStream)Convenience method to convert the document to PDF
-
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_NONENo flags passed, do the default- See Also:
- Constant Field Values
-
FLAG_EXPORT_PREFER_XSL
public static final int FLAG_EXPORT_PREFER_XSLIf available export the document using a xsl transformation- See Also:
- Constant Field Values
-
FLAG_EXPORT_PREFER_NONXSL
public static final int FLAG_EXPORT_PREFER_NONXSLIf available export the document using a visitor- See Also:
- Constant Field Values
-
FLAG_SAVE_ZIP_FILE
public static final int FLAG_SAVE_ZIP_FILESave the document in a zip container (default docx)- See Also:
- Constant Field Values
-
FLAG_SAVE_FLAT_XML
public static final int FLAG_SAVE_FLAT_XMLSave the document as a flat xml document- See Also:
- Constant Field Values
-
FLAG_SAVE_ENCRYPTED_BINARYRC4
public static final int FLAG_SAVE_ENCRYPTED_BINARYRC4RC4 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_STANDARDStandard 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_AGILEAgile 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_XMLinject 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_XMLInsert 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_SDTRemove the content controls of the document- See Also:
- Constant Field Values
-
FLAG_BIND_REMOVE_XML
public static final int FLAG_BIND_REMOVE_XMLRemove 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
-
-
Constructor Details
-
Docx4J
public Docx4J()
-
-
Method Details
-
setEventNotifier
-
load
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
Load a Docx Document from a File- Throws:
Docx4JException
-
load
public static WordprocessingMLPackage load(PackageIdentifier pkgIdentifier, java.io.File inFile) throws Docx4JExceptionLoad a Docx Document from a File, assigning it an identifier for eventing- Throws:
Docx4JException- Since:
- 3.1.0
-
load
Load a Docx Document from an InputStream- Throws:
Docx4JException
-
load
public static WordprocessingMLPackage load(PackageIdentifier pkgIdentifier, java.io.InputStream inStream) throws Docx4JExceptionLoad a Docx Document from an InputStream, assigning it an identifier for eventing- Throws:
Docx4JException- Since:
- 3.1.0
-
save
Save a Ddocx/pptx/xlsx to a File.- Throws:
Docx4JException- Since:
- 3.3.0
-
save
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
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 Docx4JExceptionSave 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 Docx4JExceptionSave 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 Docx4JExceptionSave 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 Docx4JExceptionBind 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 Docx4JExceptionBind the content controls of the passed document to the xml.- Throws:
Docx4JException
-
bind
public static void bind(WordprocessingMLPackage wmlPackage, Answers answers, int flags) throws Docx4JExceptionBind 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 Docx4JExceptionBind 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 Docx4JExceptionBind 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 Docx4JExceptionBind 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
- Throws:
Docx4JException
-
removeDefinedCustomXmlParts
protected static void removeDefinedCustomXmlParts(WordprocessingMLPackage wmlPackage, java.lang.String itemId) -
clone
public static WordprocessingMLPackage clone(WordprocessingMLPackage wmlPackage) throws Docx4JExceptionDuplicate the document- Throws:
Docx4JException
-
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 Docx4JExceptionConvert the document via xsl-fo- Throws:
Docx4JException
-
toPDF
public static void toPDF(WordprocessingMLPackage wmlPackage, java.io.OutputStream outputStream) throws Docx4JExceptionConvenience method to convert the document to PDF- Throws:
Docx4JException
-
pdfViaFO
public static boolean pdfViaFO()If the docx4j-export-fo project is present, we'll use FO for PDF export. Otherwise, we'll try to use Plutext's now-retired PDF Converter.- 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
- Throws:
Docx4JException
-
createHTMLSettings
Create the configuration object for conversions to html -
toHTML
public static void toHTML(HTMLSettings settings, java.io.OutputStream outputStream, int flags) throws Docx4JExceptionConvert 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 Docx4JExceptionConvert the document to HTML- Throws:
Docx4JException
-
getHTMLExporter
-