Read PDF file in Java using iText

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;


//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) {



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:


3) The line below gets the content of the PDF for the specified page number. The content is returned as String:

Around The World Flights - Promo Code WORLD15
String page = PdfTextExtractor.getTextFromPage(reader, 2);

4) The line below can be used to check if the document was changed in any way:


5) The line below checks if the document is encrypted:


Read PDF file in Java using iText Output:

Read PDF file in Java using iText

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)

33 responses to “Read PDF file in Java using iText”

  1. doudoun

    Good information thank you closely monitor your success

    1. Cathy

      That’s a skillful answer to a difficult question

  2. tdvofr

    This is the best article I have read, thank you, I have learned a lot of knowledge in this area.

    1. Adele

      Hey, subtle must be your middle name. Great post!

  3. bqvcbd

    nice and Favorite website

  4. Write PDF file in Java using iText - QuicklyJava

    [...] 2. Read PDF file in Java using iText [...]

  5. odzutsu

    This is a great post. Thanks so much for sharing, like always.

  6. nwnvuzwuvvy

    Best wishes!Your blog is very good!

  7. ykmihq

    Good information thank you closely monitor your success.You have to go places

  8. monarsandoval

    Thank you very much. This really helped me with my work. I appreciate your help. Thanks a lot.

  9. sidecodelink

    Easy to understand,I like it!good website

  10. tjuno


  11. herren

    JUST AWESOME!!!!!!Gr888 Job

  12. moncler

    THX for sharing.

  13. Jone

    Your posting is absolutely on the point!

  14. asmaa

    it’s very very useful than pdfbox
    thank you

  15. Aadil

    Which jar to use?I cant find the jar

  16. Tasleem

    I have problem with the code: My eclipse gives the following error:
    Please help me out.

    java.lang.UnsupportedClassVersionError: com/quicklyjava/iTextReadDemo : Unsupported major.minor version 51.0

    1. Tanya

      Hi Tasleem,

      Try this Java Library for PDF which i use, i hope it will solve your problem.

  17. uday

    I want to print all my pdf pages.. how???

  18. uday

    I want to read a single line in pdf data

  19. venkata

    good one.

  20. Maulik Patel

    Thanx it works!

  21. naan

    it worked thanx…

  22. naan

    i want to update data in pdf … how to do it?

  23. naan

    using servlet….

  24. Mustafa Shikora

    I tried using PdfTextExtractor.getTextFromPage(reader,2); but kept getting an error that said “Cannot make a static reference to the non-static method getTextFromPage(int) from the type PdfTextExtractor”

    What am I doing wrong?

  25. Drashti

    I get error com.itextpdf.text.exceptions.InvalidPdfException: PDF header signature not found.
    path is correct.I have generate this pdf using itext now try to read it but it shows error as above.

  26. itext how to check if giant string is present on the pdf page - BlogoSfera

    […] I was following this tutorial to extract the string of any of my pages: […]

  27. Abdur

    I need to resolve same issue but on JDK 1.4
    I need verify the uploaded document should be only pdf/msword. Please note that it could be possible someone rename any other file like .jpg and change to PDF. This way FileInputStream cant recognize if any file tempered.

    Please help me If you have any suggestion in jdk 1.4 to resolve such issue.


  28. cloud

    it is lovely tips..

    thank you

  29. vinayaka sy

    How to read whole content of the pdf instead of a perticular page?

Leave a Reply