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;

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:

Save 20% on hosting plans at Go Daddy
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:

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)

18 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 thanks.my 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

    Nice..#quicklyJava

  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

Leave a Reply