How to Create PDF using iText in Java?

Generating PDF report is the very general requirement in most of the Java projects. iText is the most popular PDF API used by the Java developers for generating the PDF report. It is very simple and easy to get started writing simple PDF files. If you are familiar with iText API,  it provides more advanced features to format the PDF output as you need. This tutorial explains with simple Java program that helps you to generate a PDF file with sample text and table. If you have any questions, please write it in the comments section.

1. Add iText Libraries

You can download the iText jar files from here. There is three types of JAR files.

  1. itextpdf-x.y.z.jar: the core library
  2. itext-xtra-x.y.z.jar: extra functionality
  3. itext-pdfa-x.y.z.jar: PDF/A-related functionality

2. Create PDF Conversion Utility Java File


import java.util.Date;

import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

public class PDFConversionDemo {
	    public static void main(String[] args) {
	        try {
	            OutputStream file = new FileOutputStream(new File("SamplePDF.pdf"));

	            Document document = new Document();
	            PdfWriter.getInstance(document, file);

	            document.add(new Paragraph("First iText PDF"));
	            document.add(new Paragraph(new Date().toString()));

	            document.addAuthor("Krishna Srinivasan");
	            document.addTitle("Sample PDF");

	            //Create Paragraph
	            Paragraph paragraph = new Paragraph("Title 1",new Font(Font.FontFamily.TIMES_ROMAN, 18,

	            //New line
	            paragraph.add(new Paragraph(" "));
	            paragraph.add("Test Paragraph");
	            paragraph.add(new Paragraph(" "));

	            //Create a table in PDF
	            PdfPTable pdfTable = new PdfPTable(3);
	            PdfPCell cell1 = new PdfPCell(new Phrase("Table Header 1"));

	            cell1 = new PdfPCell(new Phrase("Table Header 2"));

	            cell1 = new PdfPCell(new Phrase("Table Header 3"));

	            pdfTable.addCell("Row 1 Col 1");
	            pdfTable.addCell("Row 1 Col 2");
	            pdfTable.addCell("Row 1 Col 3");

	            pdfTable.addCell("Row 2 Col 1");
	            pdfTable.addCell("Row 2 Col 2");
	            pdfTable.addCell("Row 2 Col 3");



	        } catch (Exception e) {



3. Project Structure

It is simple Java project. Look at the project folder structure.

iText PDF Project Folder Structure

4. Output

If you run the above program, you would have a new PDF generated in the location mentioned above section. The PDF would look like this.

iText PDF Same PDF Report



About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

  • Rosey

    You can also create pdf file in java with Aspose.PDF for Java library. It offers any more features and their code samples also.

  • AmlanM

    Thanks Mr. Krishna Srinivasan. This is very helpful. Could you please write how to generate dummy pdf in java spring and sent the output to browser.