How to generate PDF report using iText java API

iText open source library which is mainly used to create, read and Manipulate the Pdf (Portable Document Format) documents.

To use iText, we need to Download iText jar file. The current version of the iText jar is 5.4.5(at the time of writing).

Also you should know how to create a project and class in Java.

How to Configure iText in Eclipse?

It is very simple as we add other jar files like selenium-server.jar or jxl.jar file. We also need to add the iText.jar file to the project in the same way.

We will show you a basic example to demonstrate the iText.

Steps to generate a pdf:

Step 1: Create a Project in eclipse
Step 2: Create a class called as "iTextPDFTutorial"
Step 3: Add iText jar file which you have downloaded.
Step 4: Add the below methods to your class.

import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;


public class iTextPDFTutorial {
	public void iTextPDF() throws Exception{
		String FILE = "D:/sampleiText.pdf";
		Document document = new Document();
		PdfWriter.getInstance(document, 
			new FileOutputStream(FILE));
		document.open();
		document.add(new Paragraph("Hello iText"));
		document.add(new Paragraph("I will be printed in PDF with the help of iText"));
		document.close();
			
	}
	
	public static void main(String args[]){
		iTextPDFTutorial get = new iTextPDFTutorial();
		try {
			get.iTextPDF();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

Code Description:

String File: We will specify the path where we need to store the Pdf file with name and extension as .pdf.
Document document = new Document() : Will create a new pdf file
PdfWriter.getInstance : Is used to take the file (FilePath).
document.open() : Will Open the pdf file.
document.add(new Paragraph("Hello iText")) : This statement will add text "Hello iText" in a pdf file.
document.add(new Paragraph("I will be printed in PDF with the help of iText")) : Will be added in the second paragraph after the first paragraph.
document.close() : After adding the content to the pdf, we need to close the pdf file.

The above code will generate the Output as
Hello iText in First paragraph and
In the second paragraph it will print the text as "I will be printed in PDF with the help of iText"

Please find the below screen shot for the pdf file generated as output.
sample iText pdf

Click here for iText Tutorial part 1 with advanced examples

iText open source library which is mainly used to create, read and Manipulate the Pdf (Portable Document Format) documents. We can generate Pdf file by using simple commands...

itext tutorial, itext tutorials, generate pdf using itext, itext pdf, how to create pdf file, how to make pdf file, itext pdf example

Reporting Tools: 

Comments

Hello,

Thanks you so much for you help. I really so happy bcz I learned so many things from this site. That will be great if you can update new things to my mail id. gopala.j47@gmail.com
With Warm Regards,
Gopal

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.