4

我有一个可填写的 PDF,其中包含 CheckBoxes、RadioButtons 和 TextBox。

我如何获得复选框名称及其值,我们如何知道它是一个复选框/单选按钮?

我正在使用 iTextSharp 并查看我的以下代码

 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
            }
        }
4

3 回答 3

4

如果您仍然需要,以下代码可能会帮助您。它仅适用于 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));
}

编辑:

此外, field.Key 是字段的名称, field.Value 是它的值。

对于复选框, if(field.Value == "Yes") 然后它被选中...如果它是其他任何东西,它不会被选中。

编辑:

我刚刚发现了如何 tro 获取单选按钮选项,如果你需要的话。

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);
于 2012-07-17T18:36:48.047 回答
4

Radio buttons, checkbox and buttons are all actually the same type of field but with different flags set. You can see the flags in the PDF Spec section 12.7.4.2.1 Table 226.

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

For a given Field you want to get the Widgets associated with it. Usually this is just one but can be more so you should account for this.

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

Button fields will have their field type (/Ft) set to /Btn so check for that

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

For the current Widget get the optional field flags (/Ff) value or use zero if it doesn't exist.

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

Then just some simple math:

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

Below is a full-working C# WinForm 2011 app targeting iTextSharp 5.2.0 that shows off all of the above looking at a file called Test.pdf living on your desktop. Just add some logic to the conditionals to handle each button type.

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();
        }
    }
}
于 2012-05-18T20:53:03.773 回答
0

带有 System.Linq 的 C# 包含用于单选按钮,获取从单选表单中的所有选项中选择的选项,还按其在 Adob​​e 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 + " ");
}
}
}
于 2017-03-23T22:20:25.477 回答