Page 1 of 1

Inserting Page-Break results in a line break

PostPosted: Mon Jan 03, 2022 10:43 pm
by klausT
Hi Jason,

We are following the advice from this post https://www.docx4java.org/forums/docx-java-f6/inserting-page-break-in-docx-file-through-docx4j-t431.html to add a page break after an AltChunk section. But this only results in a line break instead of a page break (please see attached the generated file).

We are using docx4j 11.3.2

This is the code:
Code: Select all
         // add empty line
         p = new P();
         ObjectFactory factory = Context.getWmlObjectFactory();
         Br br = factory.createBr();
         p.getContent().add(br);
         wordMLPackage.getMainDocumentPart().getContents().getContent().add(addPoint + 1, p);

         // Docu Text from Server
         wordMLPackage.getMainDocumentPart().addAltChunk(AltChunkType.Xhtml, docuText.getBytes(), addPoint + 2);
         
         // Add page break. See https://www.docx4java.org/forums/docx-java-f6/inserting-page-break-in-docx-file-through-docx4j-t431.html
         Br pageBreak = factory.createBr();
         pageBreak.setType(STBrType.PAGE);
         P para = factory.createP();
         para.getContent().add(pageBreak);
         wordMLPackage.getMainDocumentPart().getContents().getContent().add(addPoint + 3, para);


Do you have any idea what is going wrong?

Thanks, Klaus

Re: Inserting Page-Break results in a line break

PostPosted: Tue Jan 04, 2022 6:43 am
by jason
Hi Klaus

In your docx, I see:

Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
        <w:p w:rsidR="00634FA2" w:rsidP="00863110" w:rsidRDefault="00634FA2" w14:paraId="6A5D0A75" w14:textId="77777777">
            <w:pPr>
                <w:rPr>
                    <w:lang w:eastAsia="de-DE"/>
                </w:rPr>
            </w:pPr>
            <w:r>
                <w:rPr>
                    <w:lang w:eastAsia="de-DE"/>
                </w:rPr>
                <w:br w:type="page"/>
            </w:r>
        </w:p>
        <w:p>
            <w:pPr>
                <w:pStyle w:val="Heading1"/>
            </w:pPr>
            <w:bookmarkStart w:name="_Toc974054327" w:id="327"/>
            <w:r>
                <w:rPr>
                    <w:sz w:val="22"/>
                </w:rPr>
                <w:t>Header Documentation</w:t>
            </w:r>
            <w:bookmarkEnd w:id="227"/>
            <w:bookmarkEnd w:id="327"/>
        </w:p>
        <w:p/>
        <w:altChunk r:id="rId19"/>
 
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


but no <w:br> after it.

This will be because the code you found is incorrectly adding the br at the wrong level.

You need to add the Br to a run, so use:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
            // Create object for r
            R r = factory.createR();
            para.getContent().add( r);
                // Create object for br
                Br br = factory.createBr();
                r.getContent().add( br);
                    br.setType(org.docx4j.wml.STBrType.PAGE);
 
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


cheers .. Jason

Re: Inserting Page-Break results in a line break

PostPosted: Wed Jan 05, 2022 8:37 pm
by klausT
Thanks Jason for your support.
I just wanted to confirm that your proposal works.