Creating PDF using iText java based API


iText is a very popular and famous libary in existance for more than a decade now. This is an open source api provided to use with any java based application, however there are also .net version of this library available, but in this section we will only go with the Java Based application.

Tutorial Level: Beginner
API Version used for Demo: itextpdf-5.4.0
Tools required: Any suitable text editor or IDE used for java developement.

iText is in the existence for more than a decade now, and since it was born there has been so many features loaded on to it. In this article we will write a beginner level program for generating a HelloWorld.pdf.

Assuming that you have knowledge of the Java programming language, we will proceed in such a manner that we only focus on the itext rather than Java language construct.

The steps involve in pdf generation:

  1. Create Document.
  2. Get writer instance on the document.
  3. Open Document.
  4. Write content in document.
  5. Close document.

Beside these five steps we are not going to explain in this particular part. However we have plans to write more and intermediate and advance level of concepts in the matter in future.

Following program will create a very simple document with a hello world text inside. please pay attention to the steps in the program which you will find exactly like the steps mention in the points above.


import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
public class PDF {
    public static void main(String...args){
        Document document = new Document();
        try{
            PdfWriter.getInstance(document, new FileOutputStream("HelloWorld.pdf"));
            document.open();
            Paragraph p1 = new Paragraph("Hello World!");
            Paragraph p2 = new Paragraph("Examsmyantra.com is committed to provide you valuable information and tutorials on various technologies.");
				document.add(p1);
            document.add(p2);
        }
        catch(Exception e){
            System.out.println(e);
        }
        document.close();
    }
}

When you run this program this will generate a PDF which looks like the one in image below.

iText PDF

Note: When you run the program make sure you have itext jar added to the classpath of your system.

First we create a document which will serve as the base of our building pdf. After that we get the writer instance on the document, this will help to create a physical existence of the pdf we create, there are several options available for the document, example, setting size of the pdf or styling class etc. However as our focus is to provide a path map for the creation and data writing, we are not doing styling here.

Then we create two paragraphs which may contain the text and we add these paragraph to the pdf using add() method which takes various kind of elements as parameter, in this case the Paragraph object. Finally we close the pdf document. this will finalize the result and write the content to the pdf making it in editable until opened again.

Hoping that this article was easy enough to understand please feel free to leave your comments and suggestions.