3

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();
Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
  • The form is a hybrid form, meaning that you have the same form twice in the same PDF. Have you tried removing the XFA part? Read [Is it safe to remove XFA?](http://developers.itextpdf.com/question/it-safe-remove-xfa) and [How to change the text color of an AcroForm field?](http://developers.itextpdf.com/question/how-change-text-color-acroform-field) – Bruno Lowagie Sep 22 '16 at 20:48
  • Furthermore: where on earth did you find the code you are using. That code is so very wrong. Didn't you read any of the documentation? – Bruno Lowagie Sep 22 '16 at 20:54

1 Answers1

4

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • 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 availble 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 – Pavan Padavala Sep 25 '16 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 doesnot 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); Thanks for your help again. – Pavan Padavala Sep 26 '16 at 20:53
  • XFA is tricky. That's the main reason why it's being removed from the PDF specification. I fear you need specialized help from Adobe. – Bruno Lowagie Sep 27 '16 at 00:39