APACHE POI .docx文件的页数显示为零

时间:2019-02-08 07:36:36

标签: java apache apache-poi docx hwpf

我已经为Doc页面的页面计数实现了Apache POI库,但是当我将Google Doc下载为.docx文件时,页面计数为零。

编辑:我的代码如下

public Integer getPagesCount(byte[] docBytes, String type)
        throws IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(docBytes);
    String lowerFilePath = type.toLowerCase();
    if (lowerFilePath.equals("docx")) {
        @SuppressWarnings("resource")
        XWPFDocument docx = new XWPFDocument(in);
        return docx.getProperties().getExtendedProperties()
                .getUnderlyingProperties().getPages();
    } else if (lowerFilePath.equals("doc")) {
        @SuppressWarnings("resource")
        HWPFDocument wordDoc = new HWPFDocument(in);
        return wordDoc.getSummaryInformation().getPageCount();
    } else if (lowerFilePath.equals("ppt")) {
        HSLFSlideShow document = new HSLFSlideShow(in);
        return document.getSlides().size();
    } else if (lowerFilePath.equals("pptx")) {
        @SuppressWarnings("resource")
        XMLSlideShow xslideShow = new XMLSlideShow(in);
        return xslideShow.getSlides().size();
    } else if (lowerFilePath.equals("pdf")) {
        PDDocument doc = PDDocument.load(in);
        return doc.getNumberOfPages();
    }
    return 0;
}

0 个答案:

没有答案