Tony the Pony
Tony the Pony

Reputation: 41347

Determine the number of pages in a PDF file

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

Upvotes: 48

Views: 64460

Answers (6)

Sudhir Gaurav
Sudhir Gaurav

Reputation: 107

I have found a solution over google . Please find the solution bellow.

1.Either download the artifact "spire.pdf" or use the bellow dependency and repo into your pom.xml ,

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
 </repositories>

  <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>9.7.0</version>
</dependency>

use this link to find latest maven dependency. enter link description here

  1. Use java code as bellow ,

    PdfDocument pdf = new PdfDocument(Balance_Sheet_Blank-1.pdf");
     int numberOfPages=pdf.getPages().getCount();
    

Here numberOfPages is the total no of pdf page count .

Upvotes: 0

Anubhav Rawat
Anubhav Rawat

Reputation: 11

int totalPages = 0;
using (var pdfStream = file.OpenReadStream())
{
     PdfReader reader = new PdfReader(pdfStream);
     totalPages = reader.NumberOfPages;
}

Upvotes: 1

Shailesh Vikram Singh
Shailesh Vikram Singh

Reputation: 1535

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

Upvotes: 2

dogbane
dogbane

Reputation: 274532

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

Upvotes: 90

Bozho
Bozho

Reputation: 597016

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

Upvotes: 16

HamoriZ
HamoriZ

Reputation: 2438

If you generates the PDF with FOP, then you can use http://xmlgraphics.apache.org/fop/

You can count the pages with the help of fop tags.

If it is just a simple pdf file from an external source, then you should check iText API.

Upvotes: 0

Related Questions