Pavan Padavala
Pavan Padavala

Reputation: 39

How to fill an existing pdf form with itext 7 and itextsharp 5.5.9 without flattening it?

I am trying to fill out a USCIS form and after filling it is making as read only (flattening that). I am not sure why it is doing that. Even though I don’t have any code to flatten that. I searched the stack overflow and tried many different things (with itextsharp 5.5.9 and itext 7) but still it doesn’t work.

Here is the sample code I am using

string src = @"https://www.uscis.gov/sites/default/files/files/form/i-90.pdf";
    string dest = @"C:\temp\i-90Filled.pdf";

    var reader = new PdfReader(src);
    reader.SetUnethicalReading(true);
    var writer = new PdfWriter(dest);

    PdfDocument pdfDoc = new PdfDocument(reader, writer);


    // add content
    PdfAcroForm form = PdfAcroForm.GetAcroForm(pdfDoc, true);

    IDictionary<String, PdfFormField> fields = form.GetFormFields();

    PdfFormField toSet;
    
    fields.TryGetValue("form1[0].#subform[0].P1_Line3b_GivenName[0]", out toSet);
    toSet.SetValue("John");
    
    pdfDoc.Close();

From OP comments.
Further explanation (this is a "secured" XFA thus NOT an ISO standard PDF)

Thanks for your response. That is much helpful. But I have few more follow up questions If I remove the XFA form, the PDF is becoming flattened. I want it still editable. Also, I tried to fill XFA form stamper.AcroFields.Xfa.FillXfaForm(xml); It looks like there are only 55 out of total 75 fields available in the XFA form dataset. Is there any way to fill the entire form and leave it editable?? Because there is a bar code on each page which is generated based on data – Commented Sep 25, 2016 at 15:10

We need this form to be not flattened because there is a bar code at the bottom that is generated based on the form data I tried updating the XFA form with the missing fields. But still the fill does not work. XmlDocument xd = new XmlDocument(); xd.Load(xmlTemplateFile); XfaForm xform = new XfaForm(stamper.Reader); xform.DomDocument = xd; xform.Changed = true; XfaForm.SetXfa(xform, stamper.Reader, stamper.Writer); – Commented Sep 26, 2016 at 20:53

Upvotes: 2

Views: 4254

Answers (2)

Kelryu
Kelryu

Reputation: 1

This worked for me on Itext7 using C#

// Load the PDF document
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest));
// Make changes to pdf...

// Set a PdfAction at SetOpenAction that uses the Adobe's this.calculateNow() function to trigger the calculations using Javascript
pdfDoc.GetCatalog().SetOpenAction(PdfAction.CreateJavaScript("this.calculateNow();"));
// Save and close
pdfDoc.Close();

Upvotes: -1

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

Forms are filled like this with iTextSharp 5:

string src = @"https://www.uscis.gov/sites/default/files/files/form/i-90.pdf";
string dest = @"C:\temp\i-90Filled.pdf";

PdfReader pdfReader = new PdfReader(src);
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
        dest, FileMode.Create));
AcroFields form = pdfStamper.AcroFields;         
form.SetField("form1[0].#subform[0].P1_Line3b_GivenName[0]", "John");

Note that the form you are trying to fill is a hybrid form. It contains the description of the form twice: once as an AcroForm; once as an XFA form. You may want to remove the XFA form by adding this line:

form.RemoveXfa();

For more info, read the documentation:

Your code is using iText 7 for C#. If you want that code to work, you most certainly need to remove the XFA part as iText 7 doesn't support XFA. iText 7 was developed with PDF 2.0 in mind (this spec is to be released in 2017). XFA will be deprecated in PDF 2.0. A valid PDF 2.0 file will not be allowed to contain an XFA stream.

Upvotes: 4

Related Questions