How to add a new form from an existing pdf template?

Tags: templatesXFAformsiText 5

I want create a PDF template from another template. The resulting PDF should still be a template that I can fill out with data. I tried using PdfStamper but the resulting PDF is not template.

Posted on StackOverflow on Sep 11, 2013 by cgnbkm

Let's distinguish two situations, depending on the nature of your PDF template:

You are talking about an XFA template:

In this case, the PDF is merely a container for an XML stream that defines your form. The only way to change it, is by editing the XML. This is best done manually using Adobe LiveCycle Designer, but if you really want to do it programmatically, you can extract the XML from the PDF using iText, manipulate the XML using any type of XML editing software, and finally put back the XML into the PDF using iText. The programmatical solution is very difficult as it requires you to be familiar with the XFA syntax and the XFA specification consists of several hundreds of pages.

You are talking about an AcroForm template

In this case, the root dictionary has an /AcroForm dictionary of which one of the entries is a /Fields array that isn't empty. You can create a PdfReader instance for this template and pass the reader object to PdfStamper. You then create the extra fields you need (text fields, button fields,...) and add them to the stamper using the addAnnotation() method.

If this doesn't answer your question, please clarify, as it's generally not accepted to limit your question to saying "I tried using PdfStamper but the resulting PDF is not template.", you should at least show what you've tried, otherwise you risk that your question will be closed.