Pregunta

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.

¿Fue útil?

Solución

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";
      }
   }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top