Home Tutorial Java Itext Read PDF file


Share on Google+Share on Google+
Read PDF file
Posted on: May 15, 2010 at 12:00 AM
In this section, you will learn how to read a pdf file.

Read PDF file

Java provides itext api to perform read and write operations with pdf file. Here we are going to read a pdf file. For this, we have used PDFReader class. The data is first converted into bytes and then with the use of StringBuffer,it will again converted into string and display the data on the command prompt.

Here is the code:

import java.io.*;
import java.util.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class ReadPDF {
	public static void main(String[] args) throws IOException {
		try {
			Document document = new Document();
			PdfReader reader = new PdfReader("file.pdf");
			PdfDictionary dictionary = reader.getPageN(1);
			PRIndirectReference reference = (PRIndirectReference) dictionary
			PRStream stream = (PRStream) PdfReader.getPdfObject(reference);
			byte[] bytes = PdfReader.getStreamBytes(stream);
			PRTokeniser tokenizer = new PRTokeniser(bytes);
			StringBuffer buffer = new StringBuffer();
			while (tokenizer.nextToken()) {
				if (tokenizer.getTokenType() == PRTokeniser.TK_STRING) {
			String test = buffer.toString();
		} catch (Exception e) {

Related Tags for Read PDF file:

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: May 15, 2010

Recommend the tutorial

Advertisements Advertisements