Count Number of Pages in PDF in Java [2 ways]

In this post, I will be sharing how to count the number of pages in PDF in Java. There are two ways through which we can achieve our goal:
1. Using Apache PDFBox
2. Using iText

Read Also: How to Escape Character in Java

Count Number of Pages in PDF in Java

1. Using Apache PDFBox


In this method, first, we have to download the latest pdfbox-app-x.x.x.jar from here. In our case, we are using pdfbox-app-2.0.25.jar.

We will be using eclipse ide to use pdfbox-app-2.0.25.jar to write the Java program to count the number of pages in PDF.

Adding Apache PDFBox library to Eclipse


After downloading the pdfbox-app-2.0.25.jar, we have to add it to the build path:

Right click on the project > Build Path > Configure Build Path > Java Build Path > Libraries > Add External JARs > Add the pdfbox-app-2.0.25.jar downloaded from above link > Click "Apply" > Click "Apply and Close".

After adding the pdfbox-app.jar file, your window should look like this in eclipse:

Count number of pages in pdf in Java


Java Program Using Apache PDFBox


We are creating the object of PDDocument first. After that call the getNumberOfPages() method on it which returns the total number of pages present in the PDF as shown below:

Note: In the below code, we need to double the backslashes in Windows because the backslash character itself is an escape in Java literal strings.


 import java.io.IOException;
import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;

public class CountNumberOfPagesInPdf {
	public static void main(String args[]) {
		int pageCount = 0;
		PDDocument pdfDocument = null;
		// Providing Absolute path for the file in Windows OS
		String path = "C:\\Users\\SUBMITTAL\\Downloads\\code\\src\\main\\java\\com\\javahungry\\Sample.pdf";
		try 
		{
			// Load the Pdf file 
			pdfDocument = PDDocument.load(new File(path));
			pageCount = pdfDocument.getNumberOfPages();
		}
		catch(IOException ex) 
		{
			ex.printStackTrace();
		}
		System.out.println("Total number of pages in PDF is: "+ pageCount);
	}	
}


Output:
Total number of pages in PDF is: 2


2. Using iText


Add the following iText dependency to the pom.xml or we can download the latest jar files from the maven repository.
 <dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.2</version>
</dependency>


Java Program Using iText


We are creating the PDFReader class object first. Then, we are calling the getNumberOfPages() method on it which returns the total number of pages present in the PDF as shown below:
 import java.io.IOException;
import com.itextpdf.text.pdf.PdfReader;

public class CountNumberOfPagesInPdf2 {

	public static void main(String[] args) {
		String path = "C:\\Users\\SUBMITTAL\\Downloads\\code\\src\\main\\java\\com\\javahungry\\PF.pdf";
		try {
			 PdfReader pdfDoc = new PdfReader(path);
			 int pageCount = pdfDoc.getNumberOfPages();			    
			 System.out.println("Total number of pages: "+ pageCount);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


Output:
Total number of pages: 3


That's all for today, please mention in the comments in case you have any questions related to counting  the number of pages in PDF in Java.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry