Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine the number of pages in a PDF file

Tags:

java

pdf

How can I determine the number of pages in a given PDF file, using a free/open source Java API?

like image 351
Tony the Pony Avatar asked Nov 09 '10 14:11

Tony the Pony


People also ask

How do you find out the number of pages in a PDF?

The R package pdftools and the function pdf_info() provides information on the number of pages in a pdf.

How can I count pages in PDF without opening?

In Adobe Acrobat Pro, go to file > create PDF > merge files into a single PDF. Then add files and select the files you want. Click combine, and see how many pages are in the final PDF.


4 Answers

You can use Apache PDFBox to load a PDF document and then call the getNumberOfPages method to return the page count.

PDDocument doc = PDDocument.load(new File("file.pdf"));
int count = doc.getNumberOfPages();
like image 125
dogbane Avatar answered Oct 04 '22 16:10

dogbane


You should be able to do this with iText. See this thread for how to solve the problem. Here is chapter 2, which is incorrectly linked in the thread:

PdfReader reader = new PdfReader("SimpleRegistrationForm.pdf");
int pages = reader.getNumberOfPages();
like image 39
Bozho Avatar answered Oct 04 '22 16:10

Bozho


If you want to get more information about PDF, please use below code. If document does not contain any of the information, it returns null. This is pdfbox library of apache.

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDDocumentInformation;

public class DocumentService {

    public void showDocumentInfo(){
        PDDocument document= PDDocument.load(new File("file.pdf"));
        PDDocumentInformation info = document.getDocumentInformation();
        System.out.println( "Page Count=" + document.getNumberOfPages() );
        System.out.println( "Title=" + info.getTitle() );
        System.out.println( "Author=" + info.getAuthor() );
        System.out.println( "Subject=" + info.getSubject() );
        System.out.println( "Keywords=" + info.getKeywords() );
        System.out.println( "Creator=" + info.getCreator() );
        System.out.println( "Producer=" + info.getProducer() );
        System.out.println( "Creation Date=" + info.getCreationDate() );
        System.out.println( "Modification Date=" + info.getModificationDate());
        System.out.println( "Trapped=" + info.getTrapped() ); 
    }
}
like image 35
Shailesh Vikram Singh Avatar answered Oct 04 '22 18:10

Shailesh Vikram Singh


int totalPages = 0;
using (var pdfStream = file.OpenReadStream())
{
     PdfReader reader = new PdfReader(pdfStream);
     totalPages = reader.NumberOfPages;
}
like image 27
Anubhav Rawat Avatar answered Oct 04 '22 18:10

Anubhav Rawat