Manish Kumar
Manish Kumar

Reputation: 131

Page count showing zero for APACHE POI .docx file

I have implemented Apache POI library for Page count of Doc pages, but it shows page count zero when I download Google Doc as .docx file.

Edit: My code is as follows

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;
}

Upvotes: 1

Views: 784

Answers (0)

Related Questions