简体   繁体   English

如何制作多页docx?

[英]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. 从模板开始,我创建一个XWPFDocument并向其中添加表。 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. 我只是使用POI生成.docx文件的初学者,但到目前为止,我已经弄清楚了如何插入分页符。 When you have created an XWPFParagraph, you can insert a page break like this: 创建XWPFParagraph后,可以像这样插入分页符:

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 : 另一种方法是您可以使用XWPFParagraph设置分页XWPFParagraph

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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