Reputation: 11107
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(TEMPLATE);
XWPFDocument document = new XWPFDocument(is);
List<IBodyElement> elements = document.getBodyElements();
for (int i = 0; i < elements.size(); i++) {
document.removeBodyElement(i);
}
CTBody body = document.getDocument().getBody();
CTSectPr docSp = body.getSectPr();
CTPageSz pageSize = docSp.getPgSz();
CTPageMar margin = docSp.getPgMar();
BigInteger pageWidth = pageSize.getW();
pageWidth = pageWidth.add(BigInteger.ONE);
BigInteger totalMargins = margin.getLeft().add(margin.getRight());
BigInteger contentWidth = pageWidth.subtract(totalMargins);
...
XWPFTable table = document.createTable(totalRows, totalColumns);
Starting from a template I create a XWPFDocument and add a table to. How would could I add multiple tables each on a page? That is, perhaps, how do I insert a page break ?
Upvotes: 1
Views: 6427
Reputation: 21
Another way is you can set the page break using XWPFParagraph
:
XWPFDocument document = new XWPFDocument(is);
...
XWPFParagraph paragraph = document.createParagraph();
paragraph.setPageBreak(true);
Upvotes: 2
Reputation: 925
I am just a beginner using POI to generate .docx files, but I have so far figured out how to insert a page break. When you have created an XWPFParagraph, you can insert a page break like this:
XWPFDocument document = new XWPFDocument(is);
...
XWPFParagraph paragraph = document.createParagraph();
XWPFRun run = paragraph.createRun();
run.addBreak(BreakType.PAGE);
Hope this helps.
Upvotes: 6