no.of pages in pdf file

亡梦爱人 提交于 2019-12-12 02:43:48

问题


I am reading a pdf file using "pdfbox";I am not getting the total number of pages in the pdf document..I don't know why this is happening.

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(); is not giving the no.of pages..can someone help me pls...


回答1:


What you are probably looking for is this method

pdDoc.getNumberofPages();

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




回答2:


Instead of

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

call

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


来源:https://stackoverflow.com/questions/35629451/no-of-pages-in-pdf-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!