Failed to read fields from Dynamic PDF form using iTextSharp
I have used a simple dynamic pdf form generated from Adobe LiveCycle and am trying to read a field using iTextSharp 5.0 / 5.5 version using the 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();
The PDF sample can be downloaded from the link: https://forums.adobe.com/servlet/JiveServlet/download/2051245-11361/ExpandingTextBox.pdf
But I always get null fields even though I can see the field in adobe live cycle. I'm not sure what I am doing here. Any help was much appreciated.
source to share
I have used the FillXfaForm method to fill a dynamic pdf form as shown below. Before you do that, you need to make sure you are creating a dynamic PDF form in the adobe live loop.
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 anyone needs help understanding this.
source to share
Below is a sample code that I am using to extract field values ββfrom the government employment form I-9.pdf. This pdf format is xfa, similar to the above 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();
}
}
}
source to share