Raj
Raj

Reputation: 19

no.of pages in pdf file

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...

Upvotes: 0

Views: 2592

Answers (2)

exception
exception

Reputation: 618

Instead of

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

call

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

Upvotes: 0

Ronan
Ronan

Reputation: 613

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()

Upvotes: 1

Related Questions