Java iText PDF Example Tutorial

In this post, we will see a simple Java example that introduces the capability of iText Java API. We will see how to create a PDF document in Java using iText and add some contents to the PDF document. To begin with, you will have to download a latest version of iText.jar. The example that I will be providing in this post, are based on version iText-5.0.6. I hope this example will be helpful for a beginner who likes to learn about iText and its features.

There are five steps to create a PDF document with iText. We will see the code snippet for each of these steps and then see the final Java code.

1) Create a Document Object. It will be through this object a user can add contents inside a PDF document. By contents, I mean tables, paragraph, images and so on.  You can create a Document Object by using the following code snippet
Document iText_Basic_Example = new Document();
2) Obtain an instance of PdfWriter object. PdfWriter is a class in iText library that will help in writing a PDF document. You can think of PdfWriter as a writer process, that will identify the changes you do with the "Document" object and write it to the output in the form of a PDF.  This means, an instance of PdfWriter must attach itself to a "Document" object so that it can write a PDF file. We will use the Java code provided below to get an instance of PdfWriter
PdfWriter.getInstance(iText_Basic_Example, new FileOutputStream("Sample.pdf"));
3)Open the Document object. In order to write contents to a PDF file, it is required to open it first. The open() method of Document object will help to do this. I also learned that a PDF file header information will be written to the output when you open a document object. The open method can be used as shown below
iText_Basic_Example.open();
4) In step 4, we will add contents to a PDF document by using the "add" method of Document object. We create an object of type "Paragraph" and push a sample text that we want to add to the PDF file. This is just one line of code and the sample code is provided below (the writer object created in step2, writes this information to the PDF file)
iText_Basic_Example.add(new Paragraph("Basic iText Example Tutorial"));
5) Finally, we close the document by using "close" method of Document object. The PDF EOF information will be written to the document when you call this method. This signifies that the document is closed and cannot be written any further.iText will automatically close the OutputStream when the document is closed. The java code is provided below
iText_Basic_Example.close();
The complete Java code to create a PDF document using iText is provided below. This basic example will introduce a beginner to learn more about iText
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class iTextPDFTutorial {  
     public static void main(String[] args){
        try {
            Document iText_Basic_Example = new Document();
            PdfWriter.getInstance(iText_Basic_Example, new FileOutputStream("Sample.pdf"));
            iText_Basic_Example.open();
            iText_Basic_Example.add(new Paragraph("Basic iText Example Tutorial"));
            iText_Basic_Example.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
When you run this example, you will find a PDF document "Sample.pdf" generated in the folder where the class file resides. And you will find the text you added in the PDF document, when you open it..Welcome to iText ..

No comments:

Post a Comment