Java – Apache poi page breaks

apachejavams-wordpage-break

I need to create a simple word document for printing from a java program. It is necessary to have the output printed on separate pages. I'm using the following code:

XWPFDocument document = new XWPFDocument();
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.setText("TITLE");
run.addCarriageReturn();
run.setText("some text and stuff here");
run.addBreak(BreakType.PAGE);
run.setText("more text");
run.addCarriageReturn();
run.setText("one more line");

Trouble is, anything I try to insert after this page break appears on its own on a page (the last two lines of text in the above example will appear on different pages). It's as if there is now an 'auto-page-break' after every statement.
I've tried using new run, or new paragraph, but the result is always the same. Any ideas? Starting to get very frustrated here….

Best Solution

XWPFDocument document = new XWPFDocument();
...
XWPFParagraph paragraph = document.createParagraph();
paragraph.setPageBreak(true);
Related Question