Create PDF documents like a pro – part 1

Create PDF documents like a pro – part 1

November 27, 2014

For the creation of  PDF documents, we will use the iText library. It is opensource and can be downloaded here. I will create a series of blog posts in which I will describe how to manipulate PDF files through Java.

Create dynamic document

This is the most common and easy way. In this case, you don`t use any PDF templates and the new file is created at runtime from your java code. In this case iText takes care of inserting new pages when there is not enough space. Each component is added one after another to the document. As you can see I prefer using tables for layout and content but you can insert text in Paragraph, Phrases, Chunks or any other component. To complete this case you need to follow several easy steps:

  1. Create and obtain document instance with its size and margins
  2. Create PdfWriterInstance, just call the getInstance method the actual instance is not needed in this case
  3. After writer is created open the document
  4. Add the content to the document
  5. Close the document and the output stream

It is as easy as that, you can see from the provided code below:

public class FirstExample {
public static void main(String[] args) {

 1.Create and obtain document instance with its size and margins

Document document = new Document(PageSize.A4);
document.setMargins(36, 36, 36, 36);
FileOutputStream fos = null;
try {
fos = new FileOutputStream("FirstPdfDocument.pdf");

 2.Create PdfWriterInstance

Just call the getInstance method the actual instance is not needed in this case

PdfWriter writer = PdfWriter.getInstance(document, fos);

 3.After the writer is created open the document;

4.Add the content to the document

} catch (FileNotFoundException e) {
} catch (DocumentException e) {
} finally {

5.Close the document and the output stream

if (fos != null) {
try {
} catch (IOException e) {
public static void addContent(Document document) throws DocumentException {
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("Your first PDF document"));
table.addCell(cell); document.add(table);

Create document from template

Now I  will show you how to accomplish the following scenario where you have predefined template created with another program and you have to populate its form field values from the java code.  It is useful to use templates when you have static content, well-designed layout and just a few form fields to fill. Let’s look in this example as it is self-explanatory and has comments in it:

public class TemplateExample {

 1.Get some Lorem text for content

public static String getLoremText() throws IOException, DocumentException {
return       "Lorem ipsum dolor sit amet," +
"consectetur adipiscing elit. Etiam sit amet ligula pretium," +
"feugiat nisl vel, lacinia elit. Vivamus porta elit at erat" +
"gravida malesuada eu a mi. Sed sed dui est. Aliquam porta cursus eros," +
"ac imperdiet ex sagittis vel. Proin id nibh eu magna efficitur rutrum." +
"Cras a metus ipsum. Aenean quis urna dapibus, semper nulla in, cursus dolor.";
public static void main(String[] args) throws FileNotFoundException,
IOException {
File newPdfPage = new File("PageTemplateExample.pdf");
FileOutputStream fileOutputStream = new FileOutputStream(newPdfPage);
PdfReader pdfTemplate = new PdfReader(new FileInputStream("PdfPageTemplate.pdf"));

2.The only change is here we get PdfStamper object instead of PdfWriter

PdfStamper stamper = new PdfStamper(pdfTemplate, fileOutputStream);

3.This will flatten the fields once the document is closed

AcroFields acroFields = stamper.getAcroFields();

4.Get the form field by its name and set its text

acroFields.setField("TextArea", getLoremText());

Here is the example.


Ready here. Up to Part 2.

 I welcome your comments & suggestions


Denis Danov

Java EE and Oracle Developer at Dreamix

More Posts - Website

Follow Me:
TwitterLinkedInGoogle Plus

Do you want more great blogs like this?

Subscribe for Dreamix Blog now!

  • Anonymous

    we use flying saucer to build our pdf’s and it is nicer than iText. it is built on top of iText but instead writing all the formatting in java it is done in an html template where designers can tweak it easily.

  • Denis

    Thank you for your comment. We didn’t know about that option, that is why we have sticked to our current solution. Are there good materials for learning flying saucer like the “iText in Action” book?