Reading PDF form fields using iTextSharp

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

2 thoughts on “Reading PDF form fields using iTextSharp”

  1. 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. :)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>