no.of 页 pdf 文件

no.of pages in pdf file

我正在使用 "pdfbox" 阅读 pdf 文件;我没有得到 pdf 文档中的总页数。我不知道为什么会这样。

try {
    parser = new PDFParser(new FileInputStream(file));
    parser.parse();
    cosDoc = parser.getDocument();
    pdfStripper = new PDFTextStripper();
    pdDoc = new PDDocument(cosDoc);

    for (int i = 1; i <= pdDoc.getDocumentCatalog().getAllPages().size(); i++) {
        pdfStripper.setStartPage(i);
        pdfStripper.setEndPage(i);
        parsedText = pdfStripper.getText(pdDoc);
        if(i==11)
        System.out.println(parsedText/*.replaceAll("[^A-Za-z0-9. ]+", "")*/);
    }
} catch (Exception e) {
    e.printStackTrace();
    try {
        if (cosDoc != null)
            cosDoc.close();
        if (pdDoc != null)
            pdDoc.close();
    } catch (Exception e1) {
        e.printStackTrace();
    }

}

pdDoc.getDocumentCatalog().getAllPages().size();没有提供 no.of 页..有人可以帮助我吗...

您可能正在寻找的是这种方法

pdDoc.getNumberofPages();

https://pdfbox.apache.org/docs/2.0.0-SNAPSHOT/javadocs/org/apache/pdfbox/pdmodel/PDDocument.html#getNumberOfPages()

而不是

cosDoc = parser.getDocument();
...
pdDoc = new PDDocument(cosDoc);
...
...pdDoc.getDocumentCatalog().getAllPages().size()...

通话

parser.getPDDocument();
pdDoc.getNumberofPages();