How to add a new form from an existing PDF template?
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. If you are using iText 7, you can create a PdfDocument
instance for this template and pass the pdfDoc
object to PdfAcroForm.getAcroForm()
method. You then create the extra fields you need (text fields, button fields,...) and add them to the form.
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.
Click this link if you want to see how to answer this question in iText 5.