nanonymous nanonymous - 1 year ago 127
C# Question

Modify PDF Form Template with dynamic controls

I am trying here to create a piece of code which would open an existing PDF Form (previously created with Open Office) with empty controls and set the values using iTextSharp (). I am still at the testing of iTextSharp to see if it does whatever I need to do, and so far the answer is no.

Please see what I've done below according to what I found on the net:

string fileNameExisting = @"PdfTemplate.pdf";
string fileNameNew = @"new.pdf";

using (var existingFileStream = new FileStream(fileNameExisting, FileMode.Open))
using (var newFileStream = new FileStream(fileNameNew, FileMode.Create))
// Open existing PDF
var pdfReader = new PdfReader(existingFileStream);

// PdfStamper, which will create
var stamper = new PdfStamper(pdfReader, newFileStream);

var form = stamper.AcroFields;
var fieldKeys = form.Fields.Keys;

foreach (string fieldKey in fieldKeys)
bool result = form.SetField(fieldKey, "A lot of text here.");


Issue 1

iTextSharp only recognizes 'controls' elements from Open Office (Textboxes for example). I tried to add a table to the PDF Template but it doesn't appear in the fields. Which means I am really limited in what to use.

Issue 2

When I set the text in the fields, there is no wraping of the text, and the size of the controls is not dynamic which means if the text is too long, it doesn't all appear. I can't use the scroll bar as the PDF is to print.

I tried

For the first issue, I created a PDF Form with Word instead of Open Office Writer. However, iTextSharp does not recognize any of the controls from Word, my fields collection is empty..

For the second issue, I tried to modify every properties of the controls in Open Office, looked on the internet to see if someone had a solution. But from what I understood, the size is fixed as it is AcroFields, so I can't make the control dynamic and can't change the size afterwards with iTextSharp.

I was hoping someone went through the same situation and would be able to guide me either with iTextSharp, or another library, free or not. I can't afford a £2000 license though as I am running my own business, but I am open to suggestions as I need to deliver.
The last option is to create the PDF from scratch with iTextSharp, but it's not as fast and easy to produce as the modification, and it means that for every update of the PDF, the company would need me to change the code... I'm not very pleased with that solution.

Answer Source

Issue 1:

A table is not a form field. Please read the PDF specification, more specifically ISO-32000-1:

enter image description here

There is no such thing as a dynamic table in PDF. That is only possible in XFA (which is XML wrapped in a PDF file), but XFA is being deprecated. At iText, we'll release a (closed source) product in February 2017 for dynamic documents.

Issue 2:

The text only wraps if the field is defined as a multi-line text field. See for instance

The font size only adapts to the size of the field if you set the font size to 0: Set AcroField Text Size to Auto


Dynamic forms and PDF either require XFA in which case you need to buy Adobe LiveCycle ES (which is way above your budget), or you need to wait until iText Group releases its dynamics forms project (but that will also be more expensive than £2000).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download