How to create a complex PDF document?

Tags: formsXML WorkeriText 7

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 iText7 to fill in the fields like this:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(new FileOutputStream(dest)));
Document doc = new Document(pdfDoc);
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, false);
Map<String, PdfFormField> fields = form.getFormFields();

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.

Click this link if you want to see how to answer this question in iText 5.