Skip to main content
Skip table of contents

How to create a complex PDF document?

I have an Java/Java EE based application wherein I have a requirement to create PDF certificates for various services that will be provided to the users.

I am looking for a way to create PDF (no need for digital certificates for now). What is the easiest and convenient way of doing that? I have tried

  1. XSL to PDF conversion
  2. HTML to PDF conversion using itext.
  3. the crude Java way (using PdfWriter, PdfPTable, etc.)

What is the best way out of these, or is there any other way which is easier and convenient?

Posted on StackOverflow on Jan 4, 2013 by Ankit

When you talk about Certificates, I think of standard sheets that look identical for every receiver of the certificate, except for:

  • the name of the receiver,
  • the course that was followed by the receiver,
  • a date.

If this is the case, I would use any tool that allows you to create a fancy certificate (Acrobat, Open Office, Adobe InDesign,...) and create a static form (sometimes referred to as an AcroForm) containing three fields: name, course, date.

I would then use iText to fill in the fields like this:

PdfReader reader = new PdfReader(pathToCertificateTemplate);
PdfStamper stamper =
    new PdfStamper(reader, new FileOutputStream(pathToCertificate));
AcroFields form = stamper.getAcroFields();
form.setField("name", name);
form.setField("course", course);
form.setField("date", date);
stamper.setFormFlattening(true);
stamper.close();
reader.close();

Creating such a certificate from code is "the hard way"; creating such a certificate from XML is "a pain" (because XML isn't well-suited for defining a layout), creating a certificate from (HTML + CSS) is possible with iText's XML Worker, but all of these solutions have the disadvantage that it's hard work to position every item correctly, to make sure everything fits on the same page, etc...

It's much easier to maintain a template with fixed fields. This way, you only have to code once. If for some reason you want to move the fields to another place, you only have to change the template, you don't have to worry about messing around in code, XML, HTML or CSS.

Please go to the section about interactive forms to learn more about this technology.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.