Searching...
Saturday, 30 November 2013

How to read pdf document using Java and Itext

04:44

Itext PDF is an open source API that allows to create and modify pdf documents in java. In this example we will see how to read a simple pdf document using Itext.

1. Create a Maven project and add below dependency.

                 <dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.0.6</version>
		</dependency>

2. Create a Main program to read pdf


Place your pdf file in the project folder and give the file name accordingly.

package com.pretech;
import java.io.IOException;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.parser.PdfTextExtractor;
public class JavaPDFReaderExample {
	public static void main(String[] args) {
		try {
			PdfReader reader = new PdfReader("J2EEMATERIAL.pdf");
			System.out.println("Total pages " + reader.getNumberOfPages());
			String page = PdfTextExtractor.getTextFromPage(reader, 2);
			System.out.println("PDF contents:\n\n" + page + "\n\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

3. Output



Total pages 60
PDF contents:

EJB Architecture        34
Types of Beans        35
Session Beans        36
- Stateless session beans      36
- State ful session bean      37
Entity bean         39
Life cycle of Entity Bean       40
Database synchronization       40
Transactions         42
Transaction attributes       43
Bean managed transacation      46
JDBC Transactions        47
JTA Transactions        47
Handling Exceptions       48
Bean-bean Communication      49
Security          50
Understanding EJB Security      51
Authorization        52
JNDI          55

0 comments: