In this post we will read PDF file in Java using iText library.
The Java program will assume there is a PDF file at location c:/temp/test.pdf. Change this path according to your environment.
Here is the code to read PDF file in Java using iText:
package com.quicklyjava;
import java.io.IOException;
//iText imports
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class iTextReadDemo {
/**
* @param args
*/
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("c:/temp/test.pdf");
System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");
String page = PdfTextExtractor.getTextFromPage(reader, 2);
System.out.println("Page Content:\n\n"+page+"\n\n");
System.out.println("Is this document tampered: "+reader.isTampered());
System.out.println("Is this document encrypted: "+reader.isEncrypted());
} catch (IOException e) {
e.printStackTrace();
}
}
}
Explanation of some important code line:
1) The line below creates a PdfReader which we will use to read the PDF file (change the location of the PDF file as per your environment):
PdfReader reader = new PdfReader("c:/temp/test.pdf");
2) The line below gets the number of pages in the PDF:
reader.getNumberOfPages()
3) The line below gets the content of the PDF for the specified page number. The content is returned as String:
String page = PdfTextExtractor.getTextFromPage(reader, 2);
4) The line below can be used to check if the document was changed in any way:
reader.isTampered()
5) The line below checks if the document is encrypted:
reader.isEncrypted()
Read PDF file in Java using iText Output:
This way we can easily read a PDF file in Java. Further in this series of tutorials for iText we will see how to write a PDF file and also explore some additional features provided by iText.
You can download the eclipse project of the code explained in this tutorial:
iTextReadDemo (1.6MB)




Good information thank you closely monitor your success
That’s a skillful answer to a difficult question
This is the best article I have read, thank you, I have learned a lot of knowledge in this area.
Hey, subtle must be your middle name. Great post!
nice and thanks.my Favorite website
[...] 2. Read PDF file in Java using iText [...]
This is a great post. Thanks so much for sharing, like always.
Best wishes!Your blog is very good!
Good information thank you closely monitor your success.You have to go places
Thank you very much. This really helped me with my work. I appreciate your help. Thanks a lot.
Easy to understand,I like it!good website
Nice..#quicklyJava
JUST AWESOME!!!!!!Gr888 Job
THX for sharing.
Your posting is absolutely on the point!
it’s very very useful than pdfbox
thank you
Which jar to use?I cant find the jar
Aadil,
You can download the eclipse project used for this tutorial. Please find the link at the end of the article. The project has all the necessary JARs and the classpath set.