Reputation: 107
I have a PDF with forms whose values are accessible using iTextSharp 5.5.11's PDFReader.AcroFields.GetField() method. But I can't figure out how to just iterate over the fields and print the keys and values. I've tried the methods mentioned in this question: How do I enumerate all the fields in a PDF file in ITextSharp
...but no dice. I've also tried using an enumerator:
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using iTextSharp.text;
using iTextSharp.text.pdf;
class DoStuff
{
static void Main(string[] args)
{
string fileName = args[0];
PdfReader reader = new PdfReader(fileName);
AcroFields pdfFormFields = reader.AcroFields;
var enumerator = pdfFormFields.Fields.GetEnumerator();
Console.WriteLine(pdfFormFields.Fields.GetType()); // So it's a 'LinkedDictionary', how do I iterate through that and get keys and values?
while (enumerator.MoveNext()) // Evidently not like this...
{
Console.WriteLine("There are fields in the document, but this never prints");
}
}
}
...but that doesn't seem to work either. What's the current way to do this?
Upvotes: 2
Views: 1777
Reputation: 77528
You need something like this:
foreach (string key in pdfFormFields.Fields.Keys)
{
// key is the name of the field
}
If this doesn't reveal any fields, you are not looking at a form with AcroForm technology, you have an XFA form, and such a form is completely different. See How to get a list of the fields in an XFA form?
Update: if you suspect that the form is a pure XFA form, try this code:
XfaForm xfa = pdfFormFields.Xfa;
and check the value of xfa.XfaPresent
. If it's true
, you have an XFA form; if it's false, you may be confronted with a broken form. I have seen forms where there were references to widget annotations in the page dictionaires, but no references to those widget annotations in the fields array. There used to be a tool that created broken forms like this (I forgot which tool). In any case: to a human user, it looked as if there were interactive fields in the PDF, but to a machine, those weren't real fields. See ItextSharp - Acrofields are empty
Upvotes: 2