javadocx4j

Page break with DOCX4J


I need to add a table at the end of the document and the table to should start in a separate page. Can anyone help? Currently I am able to add the table at the end of the content of the existing docx file. I need to know how I can introduce a page break and start the the table at the end of the exist document in a separate page with docx4J


Solution

  • You can use a simple page break, unless you need different page size or headers/footers (in which case use a next page section break).

    For a simple page break, the XML you need is:

        <w:p>
            <w:r>
                <w:br w:type="page"/>
            </w:r>
        </w:p>
    

    Corresponding Java:

        // Create object for p
        P p2 = wmlObjectFactory.createP(); 
        body.getContent().add( p2); 
            // Create object for r
            R r = wmlObjectFactory.createR(); 
            p2.getContent().add( r); 
                // Create object for br
                Br br = wmlObjectFactory.createBr(); 
                r.getContent().add( br); 
                    br.setType(org.docx4j.wml.STBrType.PAGE);
    

    You can generate this code from a suitable sample docx (created in Word) using the Helper addin for Word, or the Docx4j Webapp.