• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

How to Create PDF using iText in Java?

October 27, 2013 //  by Krishna Srinivasan//  Leave a Comment

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

package javabeat.net.pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
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.open();
	            document.add(new Paragraph("First iText PDF"));
	            document.add(new Paragraph(new Date().toString()));

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

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

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

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

	            cell1 = new PdfPCell(new Phrase("Table Header 2"));
	            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
	            pdfTable.addCell(cell1);

	            cell1 = new PdfPCell(new Phrase("Table Header 3"));
	            cell1.setHorizontalAlignment(Element.ALIGN_CENTER);
	            pdfTable.addCell(cell1);
	            pdfTable.setHeaderRows(1);

	            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");

	            document.add(pdfTable);

	            document.close();
	            file.close();

	        } catch (Exception e) {

	            e.printStackTrace();
	        }
	    }

}

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

Category: JavaTag: iText

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.

Previous Post: « How To Convert Properties File Into XML File Using Java?
Next Post: How to Protect PDF with Password using iText in Java? »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact