How can I determine the number of pages in a given PDF file, using a free/open source Java API?
6 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();
-
4I used this in a utility method to check which documents had less than some number of pages to delete this. But then I had to close the doc with doc.close(), otherwise I couldn't delete it, because then it is closed only after some time when the doc object is deleted by the GC. Mar 17, 2019 at 19:50
-
Note: you'll run out of memory if your PDF is larger than the available memory.– dvlcubeAug 18, 2023 at 4:31
-
On pdfbox 3.0.0 u may choose to use "var doc = Loader.loadPDF(new RandomAccessReadBufferedFile(pdfFile.toAbsolutePath().toString()))" Aug 27, 2023 at 8:19
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();
-
1Note: you'll run out of memory if your PDF is larger than the available memory. You can pass a
ReaderPropertieswithsetPartialRead(true)to the constructor so the whole file won't be loaded into memory.– dvlcubeAug 18, 2023 at 4:45
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() );
}
}
-
2You might want to add the class of
document(and the library that class is from).– mklDec 11, 2018 at 5:48 -
1@mkl thanks you for valuable suggestion. I have added required information. Dec 11, 2018 at 5:59
-
1Ah, PDFBox. So you essentially propose the same approach as the accepted answer does.– mklDec 11, 2018 at 6:27
-
1This should be the accepted answer as it provides clearly the expected answer for OP's question and gives details about IMPORTS and much more information helpful to many other users. Thanks for this @ShaileshVikramSingh :-) Jun 16, 2022 at 19:02
int totalPages = 0;
using (var pdfStream = file.OpenReadStream())
{
PdfReader reader = new PdfReader(pdfStream);
totalPages = reader.NumberOfPages;
}
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.
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
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 .