Keltex
Keltex

Reputation: 26426

How do I enumerate all the fields in a PDF file in ITextSharp

Let's say I've loaded a PDF file using iTextSharp:

PdfStamper p = GetDocument();
AcroFields af = ps.AcroFields;

How do I get a list of all field names on the document from af?

Upvotes: 11

Views: 27649

Answers (4)

Bhavin Shah
Bhavin Shah

Reputation: 111

PdfReader pdfReader = new PdfReader("c:\\ABC.pdf");

string TempFilename = Path.GetTempFileName();

AcroFields pdfFormFields = pdfReader.AcroFields;

foreach (KeyValuePair<string, AcroFields.Item> kvp in pdfFormFields.Fields)
{   
        string fieldName = kvp.Key.ToString();
        string fieldValue = pdfFormFields.GetField(kvp.Key.ToString());
        Console.WriteLine(fieldName + " " + fieldValue);
}

pdfReader.Close();

Upvotes: 11

Mike
Mike

Reputation: 186

It may just be me, but I am not getting .Value anymore.

foreach (var field in af.Fields)
{
    Console.WriteLine(field.Key +" "+  af.GetField(field.Key));
}

Upvotes: 2

cecilphillip
cecilphillip

Reputation: 11586

foreach (DictionaryEntry entry in af.Fields) {
   Console.WriteLine(entry.Key +" " +entry.Value);
}

Upvotes: 2

S P
S P

Reputation: 4643

AcroFields af = ps.AcroFields;

        foreach (var field in af.Fields)
        {
            Console.WriteLine("{0}, {1}",
                field.Key,
                field.Value);
        }

Upvotes: 15

Related Questions