3

I need to insert a page break after printing some paragraphs. I found a function to insert the page break, but it inserts only an empty paragraph and not a page break.

This is the function:

private static void addPageBreak() {
    MainDocumentPart documentPart = wp.getMainDocumentPart();

    Br breakObj = new Br();
    breakObj.setType(STBrType.PAGE);

    P paragraph = factory.createP();
    paragraph.getContent().add(breakObj);
    documentPart.getJaxbElement().getBody().getContent().add(paragraph);
}

How can I fix this function to insert a full page break?

skrrgwasme
  • 9,358
  • 11
  • 54
  • 84

3 Answers3

6

Add the w:br inside a run, not directly in the w:p

P p = wmlObjectFactory.createP(); 
// Create object for r
R r = wmlObjectFactory.createR(); 
p.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 code like that using the docx4j webapp, if you have a docx containing what you want to replicate.

Adam Hughes
  • 14,601
  • 12
  • 83
  • 122
JasonPlutext
  • 15,352
  • 4
  • 44
  • 84
2

Make sure you are calling the addPageBreak() method properly. Try the following code to test your method:

public class AddingAPageBreak {
    private static ObjectFactory factory;
    private static WordprocessingMLPackage  wordMLPackage;

    public static void main (String[] args) throws Docx4JException {
        wordMLPackage = WordprocessingMLPackage.createPackage();
        factory = Context.getWmlObjectFactory();

        wordMLPackage.getMainDocumentPart().addParagraphOfText("Hello Word!");

        addPageBreak();

        wordMLPackage.getMainDocumentPart().addParagraphOfText("This is page 2!");
        wordMLPackage.save(new java.io.File("src/main/files/HelloWord11.docx") );
    }

    /**
     * Adds a page break to the document.
     */
    private static void addPageBreak() {
        MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();

        Br breakObj = new Br();
        breakObj.setType(STBrType.PAGE);

        P paragraph = factory.createP();
        paragraph.getContent().add(breakObj);
        documentPart.getJaxbElement().getBody().getContent().add(paragraph);
    }
}
Tui Popenoe
  • 2,098
  • 2
  • 23
  • 44
  • Thanks for your reply. I tested it but it does not work. It is still printing only a blank paragraph and not a page break. I am developing in a Ubuntu distro and I test it with LibreOffice. Should be that the problem? – temerariomalaga Mar 05 '15 at 14:59
  • hi @temerariomalaga , I believe you need to use windows to develop this. When I was using mac to develop the docx template, I also encountered some problems. Then I tried in windows, no problem. – Yodi S. Jan 19 '21 at 09:44
1

The documentPart.getJaxbElement().getBody().getContent().add(paragraph); way doesn't seem to work at all.

Have you tried to use documentPart.addObject(paragraph); instead?

See full example that inserts page break between two paragraphs:

public class PageBreakExample {

    private static ObjectFactory objectFactory = new ObjectFactory();

    public static void main(String[] args) throws InvalidFormatException,
        Docx4JException {
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

        // create new paragraph with a run containing text and add it to the document.
        P paragraph1 = objectFactory.createP(); // create new paragraph
        R run1 = objectFactory.createR(); // create new run 
        Text text1 = objectFactory.createText(); // create text

        text1.setValue("This is text in paragraph 1");
        run1.getContent().add(text1); // add text ton the run
        paragraph1.getContent().add(run1); // add run to paragraph
        wordMLPackage.getMainDocumentPart().addObject(paragraph1);

        addPageBreak(wordMLPackage.getMainDocumentPart());

        // proceed to create another paragraph with a run containing text.
        P paragraph2 = objectFactory.createP(); // create new paragraph
        R run2 = objectFactory.createR(); // create new run 
        Text text2 = objectFactory.createText(); // create text

        text2.setValue("This is text in paragraph 2");
        run2.getContent().add(text2); // add text ton the run
        paragraph2.getContent().add(run2); // add run to paragraph
        wordMLPackage.getMainDocumentPart().addObject(paragraph2); // add to main document part


        wordMLPackage.save(new java.io.File("two_paragraphs_page_break.docx")); // save
    }

    private static void addPageBreak(MainDocumentPart documentPart) {
        P paragraph = objectFactory.createP();
        R run = objectFactory.createR();
        P p = objectFactory.createP();
        // Create object for r
        R r = objectFactory.createR();
        p.getContent().add(r);
        // Create object for br
        Br br = objectFactory.createBr();
        r.getContent().add(br);
        br.setType(org.docx4j.wml.STBrType.PAGE);
        documentPart.addObject(p);
    }
}
Laurenzo
  • 505
  • 3
  • 20