3

I have used simple dynamic pdf form which generated from Adobe LiveCycle designer and trying to read the field using iTextSharp 5.0/5.5 version using following code.

            string pdfTemplate = @"c:\ExpandingTextBox.pdf";
            PdfReader pdfReader = null;
            pdfReader = new PdfReader(pdfTemplate);                

            StringBuilder sb = new StringBuilder();
            foreach (var de in pdfReader.AcroFields.Fields)
            {
                sb.Append(de.Key.ToString() + Environment.NewLine);
            }               
            pdfReader.Close();

Sample PDF can be downloaded from the link: https://forums.adobe.com/servlet/JiveServlet/download/2051245-11361/ExpandingTextBox.pdf

But i am always getting zero fields even though i see the field in adobe live cycle designer. I am not sure what i am doing here. Any help greatly appreciated.

Surendra Chatakondu
  • 315
  • 1
  • 4
  • 14
  • Adobe LiveCycle designer creates an XFA form while `pdfReader.AcroFields` mainly manages Acroform form, especially the `AcroFields.Fields` only contain the Acroform fields. You might want to inspect the `AcroFields` attribute `Xfa` instead. – mkl Aug 07 '14 at 05:32
  • I tried that option also and still fields count is zero. – Surendra Chatakondu Aug 07 '14 at 11:47
  • *fields count is zero* - `AcroFields.Fields` Count will remain 0; there are no AcroForm fields in your PDF. – mkl Aug 07 '14 at 14:42
  • Xfa fields count also zero. Not sure what else you are talking about. – Surendra Chatakondu Aug 07 '14 at 15:34
  • I use iText/Java version to access the information but iTextSharp/C# should be equivalent. `System.out.printf("%s", pdfReader.getAcroFields().getXfa().getTemplateSom().getName2Node());` gives me `{form1[0].#subform[0].TextFieldContainer[0].TextField1[0]=[field: null]}`. – mkl Aug 08 '14 at 14:58
  • Thanks for your comments. I have completed this task successfully using below answer. – Surendra Chatakondu Aug 12 '14 at 12:05

2 Answers2

1

I have used FillXfaForm method to fill the dynamic pdf form as show below. Before you doing this, you need to make sure you create dynamic pdf form in adobe live cycle.

        string pdfTemplate = @"c:\test.pdf";
        string newFile = @"c:\new_test.pdf";
        string xmlForm = @"C:\fill_test.xml";

            PdfReader pdfReader = new PdfReader(pdfTemplate);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(
                newFile, FileMode.Create));                
            pdfStamper.AcroFields.Xfa.FillXfaForm(xmlForm);
            pdfStamper.FormFlattening = false;

            pdfStamper.Close();
            pdfReader.Close();

Please let me know if anybody needs help in understanding this.

Surendra Chatakondu
  • 315
  • 1
  • 4
  • 14
1

The below code example is what I use to extract field values from an I-9.pdf gov't employment form. This pdf format is xfa similar to the above accepted answer and comments. Using traditional AcroFields.Fields will not work on this type of pdf form.

using System.Linq;
using iTextSharp.text.pdf;

namespace PdfFormReader
{
    class Program
    {
        static void Main(string[] args)
        {
            string pdfTemplate = @"C:\\forms\\i-9.pdf";
            PdfReader pdfReader = new PdfReader(pdfTemplate);
            var xfaFields = pdfReader.AcroFields.Xfa.DatasetsSom.Name2Node;

            foreach (var xmlNode in xfaFields)
            {
                Console.WriteLine(xmlNode.Value.Name+":"+xmlNode.Value.InnerText);
            }

            /*Example of how to get a field value*/
            var lastName = xfaFields.First(a => a.Value.Name == "textFieldLastNameGlobal").Value.InnerText;
            Console.ReadLine();
        }
    }
}
JimSTAT
  • 735
  • 1
  • 8
  • 27