1

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.");
    }

    stamper.Close();
    pdfReader.Close();
}

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.

nanonymous
  • 13
  • 5

1 Answers1

0

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 https://developers.itextpdf.com/question/how-get-row-count-multiline-field

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

Summarized:

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).

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Well I guess I will have to create the PDF from scratch then. Thanks for your answers Bruno. – nanonymous Nov 25 '16 at 12:52
  • Yes, some people use a combination of HTML / CSS and iText's XML Worker to avoid having to code the PDF. – Bruno Lowagie Nov 25 '16 at 12:56
  • if I were to buy a license for LiveCycle designer (not too expensive) to produce my Template, would I be able to do what I require with iTextSharp? I am using version 5.5.9 – nanonymous Nov 25 '16 at 15:11
  • Not really: if you want to "flatten" the template (make a regular PDF out of it), you would still need iText's XFA Worker, which is a closed source addon on top of iText. You probably want to flatten the filled out XFA template because many viewers don't know how to render XFA. In that case, you'd need to purchase a commercial license for iTextSharp and XFA Worker. – Bruno Lowagie Nov 25 '16 at 15:52