How to Extract a PDF Form Including CheckBox ( X ) Data in C#

Viewed 48

I'm working on a PDF, the main idea is to extract the pdf content including images,text as well as checkboxes, as far as the text and images I extract the text content and images but I can't able to extract the checkbox data. I have tried itextsharp and another open-source tool regarding this, unable to get the check-status ( like true or false ).

1 Answers

my c# is rusty, but using the latest version of iText, it should be something like this:

        PdfDocument doc = new PdfDocument(new PdfReader(@"c:\\temp\\form.pdf"));
        PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, false);

        IDictionary<string, PdfFormField> fields = form.GetFormFields();

        foreach (KeyValuePair<string, PdfFormField> entry in fields)
        {
            PdfFormField field = entry.Value;
            if (field is PdfButtonFormField)
            {
                Console.WriteLine(entry.Key + " has " + field.GetValueAsString());
            }
        }

where GetValueAsString() will typically have "Yes" for checked or "Off" or empty for unchecked.

Related