简体   繁体   中英

How to make a multiple pages docx?

    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 ?

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.

Another way is you can set the page break using XWPFParagraph :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM