Read checkbox, radio button name and values ​​from PDF using iText Sharp

I have a completed PDF file containing CheckBoxes and RadioButtons and TextBox.

How do I get the name of the CheckBox and its value, how do we know that this is the / Radio Button?

I am using iTextSharp and reviewing my code below

 PdfReader pdfReader = new PdfReader(FileName);
 pdfReader.SelectPages("37");
        MemoryStream oStream = new MemoryStream();
        PdfStamper stamper = new PdfStamper(pdfReader, oStream);
        AcroFields form = stamper.AcroFields;
        if (form.Fields.Count() > 0)
        {
            IDictionary<string,AcroFields.Item> oDic= form.Fields;

            foreach (string FieldName in oDic.Keys)
            {
                //FieldName - CheckBox name; i need to confirm that is a Checkbox...
            }

            foreach (AcroFields.Item oItem in oDic.Values)
            {
                // how do we get check box values
            }
        }
+3
source share
3 answers

The following code may help you if you still need to. It works only for AcroForms.

int BUTTON = 1;
int CHECK_BOX = 2;
int RADIO_BUTTON = 3;
int TEXT_FIELD = 4;
int LIST_BOX = 5;
int COMBO_BOX = 6;

PdfReader pdfReader = new PdfReader(path);
AcroFields af = pdfReader.AcroFields;

foreach (var field in af.Fields)
{
    bool isRadio = RADIO_BUTTON == af.GetFieldType(field.Key));
}

Edit:

In addition, field.Key is the name of the field and field. Value is the value in it.

For checkboxes, if (field.Value == "Yes"), then it is selected ... if it is something else, it is not selected.

Edit:

And I just found how tro get Radio Button options if you need it.

myKey k = new myKey(field.Key, af.GetField(field.Key), af.GetFieldType(field.Key));
if (k.isRadio())
{
    try { k.options.AddRange(af.GetAppearanceStates(k.key)); }
    catch { }
}
Keys.Add(k);
+4
source

, , . " PDF" 12.7.4.2.1. 226.

int ffRadio = 1 << 15;      //Per spec, 16th bit is radio button
int ffPushbutton = 1 << 16; //17th bit is push button

Field Widget. , , .

PdfDictionary w = f.Value.GetWidget(0);

(/Ft), /Btn,

if (!w.Contains(PdfName.FT) || !w.Get(PdfName.FT).Equals(PdfName.BTN)) {continue;/*Skipping non-buttons*/ }

Widget (/Ff) , .

int ff = (w.Contains(PdfName.FF) ? w.GetAsNumber(PdfName.FF).IntValue : 0);

:

if ((ff & ffRadio) == ffRadio) {
    //Is Radio
} else if (((ff & ffRadio) != ffRadio) && ((ff & ffPushbutton) != ffPushbutton)) {
    //Is Checkbox
} else {
    //Regular button
}

# WinForm 2011, iTextSharp 5.2.0, , Test.pdf, . .

using System;
using System.IO;
using System.Windows.Forms;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication3 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Test.pdf");
            PdfReader reader = new PdfReader(testFile);
            var fields = reader.AcroFields;

            int ffRadio = 1 << 15;      //Per spec, 16th bit is radio button
            int ffPushbutton = 1 << 16; //17th bit is push button
            int ff;
            //Loop through each field
            foreach (var f in fields.Fields) {
                //Get the widgets for the field (note, this could return more than 1, this should be checked)
                PdfDictionary w = f.Value.GetWidget(0);
                //See if it is a button-like object (/Ft == /Btn)
                if (!w.Contains(PdfName.FT) || !w.Get(PdfName.FT).Equals(PdfName.BTN)) { continue;/*Skipping non-buttons*/ }
                //Get the optional field flags, if they don't exist then just use zero
                ff = (w.Contains(PdfName.FF) ? w.GetAsNumber(PdfName.FF).IntValue : 0);
                if ((ff & ffRadio) == ffRadio) {
                    //Is Radio
                } else if (((ff & ffRadio) != ffRadio) && ((ff & ffPushbutton) != ffPushbutton)) {
                    //Is Checkbox
                } else {
                    //Regular button
                }
            }
            this.Close();
        }
    }
}
+3

C # with System.Linq turned on for switches, select which option is selected from all parameters in the form of a radio, also print all selection options by their specified name in Adobe Acrobat Pro

  AcroFields fields = reader.AcroFields;
 List<string> fldNames = new List<string>(fields.Fields.Keys);
            if (fldNames.Count > 0) //am gasit cel putin un acroform
            {
                foreach (string fldname in fldNames)
                {
                    int tip = fields.GetFieldType(fldname);
if (tip == 3) //choice  / radio
                    {
                        Console.WriteLine("radio form");
                        string[] valori = fields.GetListSelection(fldname);
                        foreach (string s in valori)
                            Console.WriteLine(s + " ");
                        Console.WriteLine("selected from radio form options");
                        string[] valori2 = fields.GetAppearanceStates(fldname);
                        //Console.WriteLine(valori2.Length);
                        var val2 = (from string c in valori2
                                   where (c.ToLower().CompareTo("off") != 0)
                                   select c).ToList();
                        if (val2.Count > 0)
                            foreach (string s2 in val2)
                                Console.WriteLine(s2 + " ");
}
}
}
0
source

All Articles