Get the Number of Pages in a PDF File in Java

This article demonstrates how to determine the number of pages in a PDF file using Spire.PDF for JAVA.

The following screenshot shows a PDF file which contains 73 pages:

Get the Number of Pages in a PDF File in Java

Code snippets:

import com.spire.pdf.PdfDocument;

		public class CountPagesOfPDF {

		public static void main(String[] args) {
		
		//Instantiate a PdfDocument object
		PdfDocument doc=new PdfDocument();
		//Load the PDF file
		doc.loadFromFile("Test.pdf");

		//Get the number of pages in the PDF file        
		int pageCount = doc.getPages().getCount();
        
		System.out.print(pageCount);

	}
}

Outout:

Get the Number of Pages in a PDF File in Java