Here's a quick bit of code to help you extract data from a PDF with form fields:

Open the PDF:

var pdfReader = new PdfReader(filename);

 

Read the field names:

var fieldList = GetFormFieldNames(pdfReader);

Read the field names with values:

var fieldList = GetFormFieldNamesWithValues(pdfReader);

 

The code that does all of the work:

private static string GetFormFieldNames(PdfReader pdfReader)
{
    return string.Join("\r\n", pdfReader.AcroFields.Fields
                                   .Select(x => x.Key).ToArray());
}

private static string GetFormFieldNamesWithValues(PdfReader pdfReader)
{
    return string.Join("\r\n", pdfReader.AcroFields.Fields
                                   .Select(x => x.Key + "=" + 
                                    pdfReader.AcroFields.GetField(x.Key))
                                   .ToArray());
}
 

Both of these methods will return a list of fields and possibly values, each separated by carriage return/line feed so that each field is on it's own line.

 

References:

http://stackoverflow.com/questions/4428183/concatenate-a-dictionary-to-a-string-of-key-valkey-val

http://stackoverflow.com/questions/3041883/how-do-i-enumerate-all-the-fields-in-a-pdf-file-in-itextsharp

Trackback

2 comments untill now

  1. [...] You just finished reading Reading PDF form fields using iTextSharp! Consider leaving a comment!Follow me on Twitter @mitchmilam. Read my new book: Dynamics Deep Dive: [...]

  2. Chris Bratene @ 2014-10-16 19:04

    ERROR: 'System.Collections.Hashtable' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Collections.Hashtable' could be found (are you missing a using directive or an assembly reference?)

    So the error is on the Linq Select, what am I missing? I've declared using of System.Linq, iTextSharp, iTextSharp.text.pdf, what am I missing? Any help would be apprecaited. :)

Add your comment now

Sign up for our new KnowledgeBits service and get news, tips and tricks and more, delivered straight to your inbox.