Java XWPFDocument为docx文件返回1个页数

Java XWPFDocument为docx文件返回1个页数,java,docx,Java,Docx,我正在使用XWPFDocument库查找docx文件中的页数。下面是我的代码片段: XWPFDocument docx = null; try { docx = new XWPFDocument(POIXMLDocument.openPackage(filePath)); totalPages = docx.getProperties().getExtendedProperties().getUnderlyi

我正在使用XWPFDocument库查找docx文件中的页数。下面是我的代码片段:

XWPFDocument docx = null;
            try {
                docx = new XWPFDocument(POIXMLDocument.openPackage(filePath));
                totalPages = docx.getProperties().getExtendedProperties().getUnderlyingProperties().getPages();
            } catch (IOException e) {
                logger.error("Error while loading word document at file location {}", filePath);
                totalPages = 0;
            }

对于某些.docx文件,它工作正常,但对于某些文件,它返回1,尽管有30多页。

您的代码似乎很好。尝试在catch块和dubug中为显示1页的文档添加断点。我已经检查过,没有发现异常。totalPages从totalPages=docx.getProperties().getExtendedProperties().GetUnderlineProperties().getPages()以1的形式出现;行。是否尝试使用docx.getProperties().getExtendedProperties().getPages()?该文件不包含有关“渲染后”的“实际”页数的信息。您首先需要使用Word打开文档,然后读取属性,因为Word只会在呈现(将样式应用于文本等)后进行更新。