Home > Open Source > Generating Dynamic PDF Using iText

Generating Dynamic PDF Using iText

Print Friendly Print Get a PDF version of this webpage PDF

iText is a library that allows you to generate PDF files on the fly.

In the following blog post I would show how to
1. Create PDF using PDF Template and PDF Form
2. Appending documents


Create PDF using PDF Template for PDF AcroForm:

template.pdf is a PDF template with PDF Form fields firstName and lastName.


// Read the template
InputStream inputStream = new ClassPathResource("template.pdf", DemoPdfDynamicResource.class).getInputStream();
PdfReader reader = new PdfReader(inputStream);

// Writes the modified template to http response
PdfStamper stamper = new PdfStamper(reader, output);

// Retrieve the PDF form fields defined in the template
AcroFields form = stamper.getAcroFields();

// Set values for the fields
form.setField("firstNameField", "Aparna");
form.setField("lastNameField", "Chaudhary");

// Setting this to true to make the document read-only
stamper.setFormFlattening(true);

//Close the stamper instance
stamper.close();

Appending Documents:

Here we will append all the pages from additionalInfo.pdf file to the template pdf.


InputStream inputStream = new ClassPathResource("additionalInfo.pdf", DemoPdfDynamicResource.class).getInputStream();
if (inputStream != null) {
PdfReader reader = new PdfReader(inputStream);

// get page count for additionalInfo.pdf
int totalPageCount = reader.getNumberOfPages();

// get page count for template.pdf
int templatePageCount = stamper.getReader().getNumberOfPages();

int i = 0;
while (i < totalPageCount) {
i++;

// Insert a page in the template
stamper.insertPage(templatePageCount + i, PageSize.A4);

// write the content of page from additionalInfo.pdf to the inserted page
stamper.getUnderContent(templatePageCount + i).addTemplate(stamper.getImportedPage(reader, i), 1, 0, 0, 1, 0, 0);
}
}

The advantage of using PDF templates over HTML templates is that you don’t have to invest lot of time in formatting. Also dealing with images is quite simple with PDF templates.
There are different tools available to create PDF templates and forms. If you are using Mac Platform then PDFPen is an option.

  1. November 9th, 2009 at 10:06 | #1

    great tutorial,
    only a link to iText is missing. :)

  2. Aparna Chaudhary
    November 9th, 2009 at 10:13 | #2

    Thanks Vaibhav. I just added the link.

  3. November 11th, 2009 at 16:55 | #3
  4. aro1982
    January 15th, 2010 at 11:19 | #4

    Can somebody help me with my problem? I create PDF using PDF Template for PDF AcroForm. I fill all fields like:

    form.setField(”field1″, “value”); (etc.).

    And everything it’s OK. But I also want to set text (in TextField) which size is greater than this TextField size. How can I dynamically resize TextField (after/before setField) to fit text? (I want to do this without changing text font size. I tried with something like Mutli-line but when there was more text to fit then font size was smaller.)

  5. aro1982
    January 15th, 2010 at 11:23 | #5

    And maybe you know how to get PdfTable with cells content from PDF (maybe from tagged PDF).

  6. Avinash
    May 24th, 2011 at 18:36 | #6

    hiii miss…
    i have some problem…i am doing some project,in that i require a duplicate pdf viewer for to show some content same as original pdf.(means dynamic pdf)
    Please inform me as early as possible.

  7. Guru
    June 9th, 2011 at 23:53 | #7

    If the same template is used to Print data or create a PDF document using a different paper size (different widths and heights for a page), can the template scale automatically or do you have to create a new version of the template to fit the new paper size?

  8. kp
    July 4th, 2011 at 14:14 | #8

    How can i display cotent text in each page with table in iText ?

  9. July 21st, 2011 at 09:02 | #9

    Thanks for sharing…Its helped me a lot

  1. No trackbacks yet.
Get Adobe Flash playerPlugin by wpburn.com wordpress themes