hi everyone,
i need ur help, how to count the no of pages in a pdf file using java, is it possible to do this.
if anybody knows pls send the code.
advance thanks
hidash

Dani AI

Generated

Short answer: yes — use a Java PDF library instead of reimplementing the PDF format. As hinted, libraries expose a single call to get the page count; you don’t need to parse PDF internals yourself. Below are two minimal, practical examples and a few troubleshooting notes for and responders like .

PDFBox (simple, Apache-licensed)

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

try (PDDocument doc = PDDocument.load(new File("input.pdf"))) {
    int pages = doc.getNumberOfPages();
    System.out.println("Pages: " + pages);
}

This loads the PDF and returns the number of pages via getNumberOfPages(). (pdfbox.apache.org) PDDocument javadoc Apache PDFBox.

iText 7 (reader example)

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import java.io.IOException;

try (PdfDocument pdf = new PdfDocument(new PdfReader("input.pdf"))) {
    int pages = pdf.getNumberOfPages();
    System.out.println("Pages: " + pages);
}

PdfDocument.getNumberOfPages() does the same for iText. Be aware of iText’s AGPL/commercial licensing if you distribute closed-source software. (api.itextpdf.com) PdfDocument API iText licensing FAQ.

Troubleshooting and tips

  • Encrypted files require a password when loading; code will throw an exception otherwise.
  • Very large PDFs may need streaming or careful memory handling.
  • If you use Maven, add the pdfbox dependency as shown on the project site. (pdfbox.apache.org) PDFBox dependencies.

Pick PDFBox for a permissive license and straightforward use; pick iText if its features and license model fit your project.

Recommended Answers

All 5 Replies

Google "Java PDF API" or "iText" (which is a Java PDF API, just not the only one).

i need to do using java programming. i dont want any software to do this work.
so send me the code
advance thanks
hidash

commented: Don't expect anyone here to just "send you the code". -1

Do you know what API means?

It means Application Programming Interface.

iText is a library (a jarfile) that provides you with methods for doing this. It is not a "software that does this work". But, if you want to reinvent the wheel, then please, go ahead, just don't expect anyone else to do it for you. You are going to have to inspect the actual format of PDFs (in all the varying versions) and learn how to read and manipulate them yourself. Or, you could simply download and use an API.

It worries me that you thought an API was "an application that will do what you want"... It raises the question should you be programming this in the first place? I get the impression you lack the basic know-how of Java

ha ha ha ha.. good joke....
'

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.