Class DocxTool


  • public class DocxTool
    extends java.lang.Object
    DOCX toolbox
    • Constructor Summary

      Constructors 
      Constructor Description
      DocxTool()
      Empty constructor, use newDocument to generate the package.
      DocxTool​(byte[] b)
      Load from docx byte array
      DocxTool​(java.io.File file)
      Load from docx file
      DocxTool​(java.io.InputStream in)
      Load from docx stream
    • Method Summary

      Modifier and Type Method Description
      boolean add​(java.lang.Object o)
      Add something at the main part
      boolean add​(java.util.List<java.lang.Object> list)
      Add objects at the main part
      void addFieldBegin​(org.docx4j.wml.P paragraph, org.docx4j.wml.ObjectFactory objectFactory)  
      void addFieldEnd​(org.docx4j.wml.P paragraph, org.docx4j.wml.ObjectFactory objectFactory)  
      void addHTML​(java.lang.String html)
      Add an HTML content shorthand
      void addNumberedParagraph​(java.lang.String style, long numId, long level, java.lang.String text)
      Add a numbered paragraph
      void addNumberedParagraph​(java.lang.String style, long numId, long level, java.lang.String text, int bookmarkId, java.lang.String bookmarkName)
      Add a numbered paragraph with bookmark
      void addNumbering()
      Add the default numbering part
      void addNumbering​(java.lang.String xml)
      Add a numbering part
      void addPageBreak()  
      void addParagraph​(java.lang.String text)  
      void addStyledParagraph​(java.lang.String style, java.lang.String text)
      Add a styled paragraph
      void addTableOfContent​(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart documentPart, java.lang.String depth)
      Add table of contents
      void addTableOfContentField​(org.docx4j.wml.P paragraph, org.docx4j.wml.ObjectFactory objectFactory, java.lang.String depth)  
      void addXHTML​(java.lang.String xhtml)
      Add an XHTML content shorthand
      java.util.List<java.lang.Object> convert​(java.lang.String xhtml)
      Convert the XHTML to a list of docx objects.
      void createBookmark​(org.docx4j.wml.P p, org.docx4j.wml.R r, java.lang.String bookmark, int id)
      Surround the specified run in the specified paragraph with a bookmark
      org.docx4j.openpackaging.parts.WordprocessingML.FooterPart createFooter()
      Add a footer to the document
      org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart createHeader()
      Add a header to the document
      java.util.List<java.lang.Object> getContent()
      Get all main contents
      java.util.List<org.docx4j.openpackaging.parts.WordprocessingML.FooterPart> getFooters()
      Get the document footer parts
      java.util.List<org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart> getHeaders()
      Get the document header parts
      org.docx4j.wml.P.Hyperlink getHyperlink​(java.lang.String bookmark, java.lang.String label)
      Create a link to a bookmark (w:hyperlink)
      org.docx4j.wml.P getImage​(byte[] bytes, java.lang.String filenameHint, java.lang.String altText, int id1, int id2)
      Build a simple image in a paragraph
      org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart getMainPart()
      Get the main document part
      org.docx4j.wml.PPr getNum​(java.lang.String style, long numId, long level)  
      org.docx4j.wml.P getNumberedParagraph​(java.lang.String style, long numId, long level, java.lang.String text)  
      org.docx4j.wml.P getNumberedParagraph​(java.lang.String style, long numId, long level, java.lang.String text, int bookmarkId, java.lang.String bookmarkName)  
      org.docx4j.openpackaging.packages.WordprocessingMLPackage getPackage()
      Get the whole document
      org.docx4j.wml.Br getPageBreak()
      Build a page break
      org.docx4j.wml.P getParagraph​(java.lang.String text)
      Build a simple paragraph
      org.docx4j.wml.R getRun​(java.lang.String text)
      Build a simple run
      org.docx4j.wml.PPr getStyle​(java.lang.String style)  
      org.docx4j.wml.P getStyledParagraph​(java.lang.String style, java.lang.String text)  
      java.lang.String getText​(java.lang.Object obj)
      Get the first text in the docx object
      org.docx4j.wml.Text getText​(java.lang.String text)
      Build a simple text
      java.util.List<java.lang.String> getTexts()
      Get all texts in the document
      java.util.List<java.lang.String> getTexts​(java.lang.Object obj)
      Get all texts in the docx object
      org.docx4j.wml.P getTOCparagraph​(java.lang.String depth)  
      static javax.xml.bind.JAXBElement<?> getWrappedFldChar​(org.docx4j.wml.FldChar fldchar)  
      java.lang.String getXmlNumbering​(int numId)
      Return w:numbering schema
      void insert​(int index, java.lang.Object o)
      Insert something in the main part
      void insertPageBreak​(int index)  
      void insertParagraph​(int index, java.lang.String text)  
      java.lang.String mainPartToString​(boolean suppDecl, boolean prettyPrint)
      Convert the main part into XML
      void newDocument()
      Create a new default document
      void newDocument​(org.docx4j.model.structure.PageSizePaper sz, boolean landscape)
      Create a new document
      void open​(java.io.File file)
      Open the document
      void open​(java.io.InputStream in)
      Open the document
      java.lang.Object remove​(int index)
      Remove something in the main part
      java.lang.Object remove​(java.lang.Object o)
      Remove something in the main part
      void replace​(java.lang.Object part, java.lang.String toFind, java.lang.String replacer)  
      void replace​(java.lang.String toFind, java.lang.String replacer)  
      void replaceBookmark​(org.docx4j.wml.P p, org.docx4j.wml.R r, java.lang.String label, java.lang.String bookmarkName)
      Surround the label with a bookmark text
      void save​(java.io.File file)
      Write the document into file
      void save​(java.io.OutputStream out)
      Write the document into output stream
      java.util.List<java.lang.Object> scan​(java.lang.Object obj, java.lang.Class<?> cls)
      Search objects corresponding to the class
      java.util.List<java.lang.Object> scan​(java.lang.String xpath)
      Search objects corresponding to the xpath
      void stringToMainPart​(java.lang.String xml)
      Replace the main part with XML
      byte[] toByteArray()
      Get the document as a byte array (e.g.
      java.lang.String toXHTML​(java.io.InputStream input, java.lang.String encoding)
      Convert any HTML to a well-formatted XHTML (jtidy implementation)
      java.lang.String toXHTML​(java.lang.String html)
      Convert any HTML to a well-formatted XHTML
      java.lang.String toXHTML​(java.lang.String html, java.lang.String encoding)
      Convert any HTML to a well-formatted XHTML
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DocxTool

        public DocxTool()
        Empty constructor, use newDocument to generate the package.
      • DocxTool

        public DocxTool​(java.io.File file)
        Load from docx file
      • DocxTool

        public DocxTool​(java.io.InputStream in)
        Load from docx stream
      • DocxTool

        public DocxTool​(byte[] b)
        Load from docx byte array
    • Method Detail

      • open

        public void open​(java.io.File file)
        Open the document
        Parameters:
        file - Docx file
      • open

        public void open​(java.io.InputStream in)
        Open the document
        Parameters:
        in - Docx input stream
      • newDocument

        public void newDocument()
        Create a new default document
      • newDocument

        public void newDocument​(org.docx4j.model.structure.PageSizePaper sz,
                                boolean landscape)
        Create a new document
        Parameters:
        sz - default is PageSizePaper.A4
        landscape - horizontal or not
      • addNumbering

        public void addNumbering()
        Add the default numbering part
      • addNumbering

        public void addNumbering​(java.lang.String xml)
        Add a numbering part
        Parameters:
        xml - Numbering definition ex: getXmlNumbering(1234);
      • getPackage

        public org.docx4j.openpackaging.packages.WordprocessingMLPackage getPackage()
        Get the whole document
      • getMainPart

        public org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart getMainPart()
        Get the main document part
      • getContent

        public java.util.List<java.lang.Object> getContent()
        Get all main contents
      • mainPartToString

        public java.lang.String mainPartToString​(boolean suppDecl,
                                                 boolean prettyPrint)
        Convert the main part into XML
      • stringToMainPart

        public void stringToMainPart​(java.lang.String xml)
                              throws javax.xml.bind.JAXBException
        Replace the main part with XML
        Throws:
        javax.xml.bind.JAXBException
      • getHeaders

        public java.util.List<org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart> getHeaders()
        Get the document header parts
      • createHeader

        public org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart createHeader()
        Add a header to the document
      • getFooters

        public java.util.List<org.docx4j.openpackaging.parts.WordprocessingML.FooterPart> getFooters()
        Get the document footer parts
      • createFooter

        public org.docx4j.openpackaging.parts.WordprocessingML.FooterPart createFooter()
        Add a footer to the document
      • scan

        public java.util.List<java.lang.Object> scan​(java.lang.String xpath)
        Search objects corresponding to the xpath
        Parameters:
        xpath - examples: //w:t to search all texts; //w:p paragraphs...
      • scan

        public java.util.List<java.lang.Object> scan​(java.lang.Object obj,
                                                     java.lang.Class<?> cls)
        Search objects corresponding to the class
        Parameters:
        obj - starting point
        cls - examples: Text.class for texts, P.class to search paragraphs...
      • getText

        public java.lang.String getText​(java.lang.Object obj)
        Get the first text in the docx object
      • getTexts

        public java.util.List<java.lang.String> getTexts​(java.lang.Object obj)
        Get all texts in the docx object
      • getTexts

        public java.util.List<java.lang.String> getTexts()
        Get all texts in the document
      • replace

        public void replace​(java.lang.String toFind,
                            java.lang.String replacer)
      • replace

        public void replace​(java.lang.Object part,
                            java.lang.String toFind,
                            java.lang.String replacer)
      • add

        public boolean add​(java.lang.Object o)
        Add something at the main part
      • add

        public boolean add​(java.util.List<java.lang.Object> list)
        Add objects at the main part
      • insert

        public void insert​(int index,
                           java.lang.Object o)
        Insert something in the main part
      • remove

        public java.lang.Object remove​(int index)
        Remove something in the main part
      • remove

        public java.lang.Object remove​(java.lang.Object o)
        Remove something in the main part
      • getText

        public org.docx4j.wml.Text getText​(java.lang.String text)
        Build a simple text
      • getRun

        public org.docx4j.wml.R getRun​(java.lang.String text)
        Build a simple run
      • getParagraph

        public org.docx4j.wml.P getParagraph​(java.lang.String text)
        Build a simple paragraph
      • addParagraph

        public void addParagraph​(java.lang.String text)
      • insertParagraph

        public void insertParagraph​(int index,
                                    java.lang.String text)
      • getPageBreak

        public org.docx4j.wml.Br getPageBreak()
        Build a page break
      • addPageBreak

        public void addPageBreak()
      • insertPageBreak

        public void insertPageBreak​(int index)
      • getImage

        public org.docx4j.wml.P getImage​(byte[] bytes,
                                         java.lang.String filenameHint,
                                         java.lang.String altText,
                                         int id1,
                                         int id2)
                                  throws java.lang.Exception
        Build a simple image in a paragraph
        Throws:
        java.lang.Exception
      • getStyle

        public org.docx4j.wml.PPr getStyle​(java.lang.String style)
      • getNum

        public org.docx4j.wml.PPr getNum​(java.lang.String style,
                                         long numId,
                                         long level)
      • getNumberedParagraph

        public org.docx4j.wml.P getNumberedParagraph​(java.lang.String style,
                                                     long numId,
                                                     long level,
                                                     java.lang.String text)
      • getNumberedParagraph

        public org.docx4j.wml.P getNumberedParagraph​(java.lang.String style,
                                                     long numId,
                                                     long level,
                                                     java.lang.String text,
                                                     int bookmarkId,
                                                     java.lang.String bookmarkName)
      • addNumberedParagraph

        public void addNumberedParagraph​(java.lang.String style,
                                         long numId,
                                         long level,
                                         java.lang.String text)
        Add a numbered paragraph
        Parameters:
        style - Default is Normal
        numId - Numbering id
        level - Level
        text - Text
      • addNumberedParagraph

        public void addNumberedParagraph​(java.lang.String style,
                                         long numId,
                                         long level,
                                         java.lang.String text,
                                         int bookmarkId,
                                         java.lang.String bookmarkName)
        Add a numbered paragraph with bookmark
        Parameters:
        style - Default is Normal
        numId - Numbering id
        level - Level
        text - Text
      • addStyledParagraph

        public void addStyledParagraph​(java.lang.String style,
                                       java.lang.String text)
        Add a styled paragraph
        Parameters:
        style - Default is Normal
        text - Text
      • getStyledParagraph

        public org.docx4j.wml.P getStyledParagraph​(java.lang.String style,
                                                   java.lang.String text)
      • createBookmark

        public void createBookmark​(org.docx4j.wml.P p,
                                   org.docx4j.wml.R r,
                                   java.lang.String bookmark,
                                   int id)
        Surround the specified run in the specified paragraph with a bookmark
        Parameters:
        p - paragraph
        r - run
        bookmark - bookmark name
        id - bookmark id
      • getHyperlink

        public org.docx4j.wml.P.Hyperlink getHyperlink​(java.lang.String bookmark,
                                                       java.lang.String label)
        Create a link to a bookmark (w:hyperlink)
        Parameters:
        bookmark - bookmark name
        label - hyperlink label
        Returns:
        Hyperlink link to add somewhere
      • replaceBookmark

        public void replaceBookmark​(org.docx4j.wml.P p,
                                    org.docx4j.wml.R r,
                                    java.lang.String label,
                                    java.lang.String bookmarkName)
        Surround the label with a bookmark text
      • convert

        public java.util.List<java.lang.Object> convert​(java.lang.String xhtml)
        Convert the XHTML to a list of docx objects.
      • toXHTML

        public java.lang.String toXHTML​(java.lang.String html)
        Convert any HTML to a well-formatted XHTML
        Parameters:
        html - Source
        Returns:
        XHTML or empty string in case of error
      • toXHTML

        public java.lang.String toXHTML​(java.lang.String html,
                                        java.lang.String encoding)
        Convert any HTML to a well-formatted XHTML
        Parameters:
        html - Source
        encoding - Use the platform encoding when null
        Returns:
        XHTML or empty string in case of error
      • toXHTML

        public java.lang.String toXHTML​(java.io.InputStream input,
                                        java.lang.String encoding)
        Convert any HTML to a well-formatted XHTML (jtidy implementation)
        Parameters:
        input - HTML as input stream
        encoding - Use the platform encoding when null
        Returns:
        XHTML or empty string in case of error
      • addXHTML

        public void addXHTML​(java.lang.String xhtml)
        Add an XHTML content shorthand
      • addHTML

        public void addHTML​(java.lang.String html)
        Add an HTML content shorthand
      • addTableOfContent

        public void addTableOfContent​(org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart documentPart,
                                      java.lang.String depth)
        Add table of contents
        Parameters:
        documentPart - document part
      • getTOCparagraph

        public org.docx4j.wml.P getTOCparagraph​(java.lang.String depth)
      • addTableOfContentField

        public void addTableOfContentField​(org.docx4j.wml.P paragraph,
                                           org.docx4j.wml.ObjectFactory objectFactory,
                                           java.lang.String depth)
      • addFieldBegin

        public void addFieldBegin​(org.docx4j.wml.P paragraph,
                                  org.docx4j.wml.ObjectFactory objectFactory)
      • addFieldEnd

        public void addFieldEnd​(org.docx4j.wml.P paragraph,
                                org.docx4j.wml.ObjectFactory objectFactory)
      • getWrappedFldChar

        public static javax.xml.bind.JAXBElement<?> getWrappedFldChar​(org.docx4j.wml.FldChar fldchar)
      • getXmlNumbering

        public java.lang.String getXmlNumbering​(int numId)
        Return w:numbering schema
      • save

        public void save​(java.io.File file)
        Write the document into file
      • save

        public void save​(java.io.OutputStream out)
        Write the document into output stream
      • toByteArray

        public byte[] toByteArray()
        Get the document as a byte array (e.g. for usage as publication template or external objet output)