2

I am working on a PDF Filler application that will populate fields in a PDF with values from a web form. My code works fine for older PDFs that I have but new ones that have been created with Adobe LiveCycle Designer don't seem to be able to see the fields in the PDF. It was suggested to save the PDF as "Adobe Static PDF Form" but this did not fix the issue.

Here is my code:

        Doc doc = new Doc();
        doc.Read(Server.MapPath("~/pdfs/test.pdf"));

        foreach (Field field in doc.Form.Fields)
        {
            if (field.Name == "StreetAddress")
            {
                field.Value = StreetAddress.Text;
            }
        }

In my code doc.Form.Fields contains only 1 field that has the value 'form1[0]' for its name even though there actually 7 fields in the PDF.

Any help would be greatly appreciated.

brookesmash
  • 95
  • 2
  • 11
  • I have been looking into this a little more and discovered that it seems like Adobe LiveCycle creates PDFs with layers. This causes the field name to go from 'StreetAddress' to form1[0].#subform[0].StreetAddress[0]. Is there any way to have Adobe Live Cycle keep the original Field name 'StreetAddress'. – brookesmash Jun 06 '12 at 20:17
  • 1
    I suggest that you would better contact the support for this issue. Or you can google for an alternative. How do you think? –  Jun 07 '12 at 03:38
  • I did both of these already with little help but I have figured out a solution for this finally. – brookesmash Jun 07 '12 at 14:20

1 Answers1

2

I have figured out the answer to this after a lot of playing around with the code. Use the GetFieldNames() function to get an array of field names because this will return all the fields no matter the level. With this you can loop through the array creating Fields by the name. The only other issue I had was that the field name is still in the format "form1[0].#pageSet[0].Page1[0].StreetAddress[0]" but if you use the PartialName property it will return "StreetAddress[0]" so then you just need to remove the last 3 characters to get the proper field name.

    string[] fieldNames = doc.Form.GetFieldNames();
    foreach (string fieldName in fieldNames)
    {
      Field field = doc.Form[fieldName];
      if(field.PartialName.Substring(0, field.PartialName.Length - 3).ToLower().Equals("streetaddress"))
      {
        field.Value = "whatever value you want";
      }
   }
brookesmash
  • 95
  • 2
  • 11