Search This Blog

Saturday, 28 February 2015


iText is an API for creating and manipulating PDF files in java.

Note: you must add itextpdf jar file to your build path in order to use itext pdf API.

Download Link:



Using Maven:

If you are interested to use a maven dependency - add the following to your dependencies list:


Note: you can continue reading this post even if you don't no maven.

If you are new to maven - click here ---> What is Maven & how to use it?

This post explains the creation of PDF with a specifed format like this:

Example PDF :  CWCReport.pdf

Important Classes:

Mostly all the classes I am using here are from same package:
com.itextpdf.text package

>>Document - Objects of this classes resembles the real world PDF document.

//creating an instance of Document
Document document = new Document();

Document class has different overloaded constructors. I am using one which specifies the page size of A4.

//creating an instance of Document
Document document = new Document(PageSize.A4.rotate());

>>PageSize  - class having all standard page formats like A1-A10 , B1-B10

rotate() method - creates a landscape PDF.


* After defining a Document object - define PdfWriter object

//creating PdfWriter instance using a static method getInstance

//You can specify the path for PDF file creating using FileOutputStream
 OutputStream file=new FileOutputStream(new File("/home/dinesh/CWCReport.pdf"));

For File pass you pdf path where you want to create on your file system.

Note: On windows, you may have you give \\ (double slashes) for file's path.
Like: c:\\home\\pdfs\\create.pdf

//Parameters document & FileOutStream file
PdfWriter.getInstance(document, file);

Defining Document Information using document object:

using document object - we can define the pdf author, subject, title, & creation date.

                 //defining an author
                document.addAuthor("Dinesh Dontha");

                 //defining a subject
                document.addSubject("CWC Matches List");
                // title on the document-window
                document.addTitle("CWC - Schedule");

                // creation date

Opening a Document:

Note: Inorder to write content into the pdf document - you must open it first

//open a document;

Adding a Paragraph:

>> Paragraph

Objects of this classes resembles real-object Paragraphs - so to create a paragraph use an object of it.

Paragraph class extends a Phrase class.

Phrase is like a sentence.

Defining Paragraphs :

//create a paragraph - using a String argument
Paragraph para2 = new Paragraph("para1....");

//create a paragraph -using a Phrase

Phrase is a series of chunks

Here, we also have Chunk class

Chunk is the smallest significant part of the text that can be added to

//creating a phrase with no parameters
Phrase phrase = new Phrase();

//Phrase with a specified font
Phrase title =new Phrase("phrase1",new Font(FontFamily.HELVETICA, 32));


>>Font  class 

 FontFamily  is an ENUM defined inside Font class

//creating Paragraph by passing Phrase instance
Paragraph para2 = new Paragraph(phrase);

//creating Paragraph with specified Font
Paragraph para2 = new Paragraph("Paragraph Text", new Font(FontFamily.HELVETICA, 16));

 Adding New Blank Lines between Paragraphs:

Using Chunk class


//adds a blank line

Creating a table:

Use  PdfPTable class instance


While creating the instance of PdfPTable - we can define the number of columns in that table

//create a table with 2 columns
PdfPTable table = new PdfPTable(2);

Using this instance, you can set some global properties for this table elements

//setting the table width in the document

Here, table extends complete width of page.

 //setting a padding for all cells

//setting an align of cell data      

Defining Table Cells:

we can add table cells using  addCell() method of PdfPTable class

//adding a basic cell using addCell() method

table.addCell("Match Date");


you can define table cells using PdfPCell class

PdfPCell class has many overloaded constructors

//with no parameters
PdfPCell cell1 = new PdfPCell();

//with a Phrase paramter -defines the cell content with Phrase
Phrase p1 =new Phrase("Cell Content");

//defining cell with phrase as content
PdfPCell cell = new PdfPCell(p1);

Once defined a cell instance - we can override the properties that are
globally defined- defined on table i.e, PdfPTable object.

//overrides padding defined on table object

 //setting properties on PdfPTable object-"table" are not applicable for this particular cell defined using PdfPCell class

i.e,       table.getDefaultCell().setPadding(10);

Here, padding for all cells is 10. But, for this cell - it is 20.

//similarly this overrides - alignment property set on table(PdfPTable) object


All the data in this example are stored in a Map object.

Note: finally close the document object and file stream object.

import java.util.HashMap;
import java.util.Map;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.PageSize;
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;

 * Created on 26-Sep-2014

public class CreatePdf {

 public static void main(String[] args) {

try {

 // creating a file stream object
 OutputStream file = new FileOutputStream(new File(

 // creating a document object - with A4 size
 // please see the effect of rotate() method -with and without it

 // rotate() method -creates a pdf in landscape format
 Document document = new Document(PageSize.A4.rotate());

 // must create a PdfWriter object -pass document and file output
 // stream
 PdfWriter.getInstance(document, file);

 // creating document info
 document.addAuthor("Dinesh Dontha");
 document.addSubject("CWC Matches List");
 document.addTitle("CWC - Schedule");

 // must open a document -to put content;

 // creating a Phrase
 Phrase title = new Phrase("Cricket World Cup-2015- Schedule",
   new Font(FontFamily.HELVETICA, 32));

 // creating a Paragrapgh with Phrase as parameter
 Paragraph para1 = new Paragraph(title);

 // adding paragraph to document -this main heading for document.

 // adding a blank line after - heading/title

 // creating a paragraph
 Paragraph para2 = new Paragraph(
   "Next upcoming league matches of Team India in the "
     + "giagantic Cricket World Cup-2015", new Font(
     FontFamily.HELVETICA, 16));

 // adding a paragraph - this is infact the first paragraph in this
 // exxample

 // adding a blank line after - first paragraph

 // adding all cell data to a Map object
 Map indiaMatches = new HashMap<>();

 indiaMatches.put("Sunday, March 6th 2015", "INDIA VS WEST INDIES");
 indiaMatches.put("Sunday, March 10th 2015", "INDIA VS IRELAND");
 indiaMatches.put("Sunday, March 14th 2015", "INDIA Vs ZIMBABWE");

 // Define a PDF table with 2 columns - Match Date & Between column
 // Names
 PdfPTable table = new PdfPTable(2);

 // Setting global table cell properties

 // table header creation -using PdfPCell class

 // creating table header -cell -first column - Match Date
 Phrase matchDate = new Phrase("Match Date", new Font(
   FontFamily.HELVETICA, 14, Font.BOLD));
 // using PdfPCell class
 PdfPCell cellHeader1 = new PdfPCell(matchDate);

 // creating table header -cell -second column - Between
 Phrase matchBetween = new Phrase("Match Between", new Font(
   FontFamily.HELVETICA, 14, Font.BOLD));
 // using PdfPCell class
 PdfPCell cellHeader2 = new PdfPCell(matchBetween);

 // creating cell data -other than table column headers- using
 // addCell()
 // method of PdfPTable object
 for (Map.Entry entry : indiaMatches.entrySet()) {

 // adding table to Pdf document

 // finally close - document & file stream

} catch (DocumentException | IOException e) {
 // TODO Auto-generated catch block


Source Code: Download
Note: To run it successfully change the file path.

No comments:

Post a Comment