页脚中的文档总页数计算不正确(Java)

问题描述 投票:0回答:1

晚上好 我正在创建一个小程序来创建一批文档。 在每个创建的文档的页脚中,我尝试反映当前页面的序列号和文档中的总页数,即提供诸如“第 1 页,共 3 页”之类的行。 但是,不可能正确反映一份文档中的总页数。页脚显示整个文档包的总页数。

这是我的代码的一部分:

XWPFHeaderFooterPolicy headerFooterPolicy = firstDocument.getHeaderFooterPolicy();
        if (headerFooterPolicy == null) headerFooterPolicy =         firstDocument.createHeaderFooterPolicy();
        
        XWPFFooter footer = headerFooterPolicy.createFooter(XWPFHeaderFooterPolicy.DEFAULT);
        XWPFParagraph parafooter = footer.createParagraph();
        parafooter.setAlignment(ParagraphAlignment.RIGHT);
        XWPFRun runfooter = parafooter.createRun();
        runfooter.setText("First line");
        runfooter.setFontFamily("Times New Roman");
            
        XWPFParagraph parafooter1 = footer.createParagraph();
        parafooter1.setAlignment(ParagraphAlignment.RIGHT);
        XWPFRun runfooter1 = parafooter1.createRun();
        runfooter1.setText("page no. ");
        runfooter1.getCTR().addNewPgNum();
        runfooter1=parafooter1.createRun();
        runfooter1.setText(" of " + firstDocument.getBodyElements().size());
        runfooter1.setFontFamily("Times New Roman");
        
        firstDocument.write(new FileOutputStream(new File(fileString + "\\FirstFile.doc")));
        firstDocument.close();

这是出现在页脚中的结果:

“第一行 页码1 个 41 英寸

我尝试使用不同的功能,但没有取得积极的结果。 我将非常感谢您的帮助。

java file netbeans footer document
1个回答
0
投票

您使用的方法正确吗

firstDocument.getBodyElements().size()
?你试过
firstDocument.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
吗?

您可以查看此处此处了解计算页面的不同方法。让我们知道是否有任何对您有用..

© www.soinside.com 2019 - 2024. All rights reserved.