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 + "=" + 

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.



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

  2. Thanks for your code. Really helped.
    Chris : pdfReader.acroFields.Fields is a Hashtable. To use Linq, you can convert it to a Dictionary.

    public static string GetFormFieldNamesWithValues(PdfReader pdfReader)
    var dictionary = HashtableToDictionary(pdfReader.AcroFields.Fields);
    return string.Join("\r\n",
    .OrderBy(d => d.Key)
    .Select(x => x.Key + " = " + pdfReader.AcroFields.GetField(x.Key))

    public static Dictionary HashtableToDictionary (Hashtable table)
    return table
    .Cast ()
    .ToDictionary (kvp => (K)kvp.Key, kvp => (V)kvp.Value);

Leave a Reply

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