2

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?

user2686455
  • 97
  • 2
  • 7
  • Looks like in [this](https://stackoverflow.com/questions/3367390/how-to-read-pdf-form-data-using-itextsharp) thread you have the answer you need. – Rigerta May 22 '17 at 19:20

1 Answers1

2

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

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Well, the foreach loop revealed nothing so I guess it's XFA. Thanks for the info. – user2686455 May 23 '17 at 13:01
  • I remember that there is another possibility. I have updated my answer with a link to a question from someone who had the same problem you have. – Bruno Lowagie May 23 '17 at 14:58
  • Thanks. XfaPresent is true, so that solves the mystery. I took a look at the first link you sent and it appears I'll have to delve into Xml... – user2686455 May 23 '17 at 21:38