4

チェックボックスとラジオボタンとテキストボックスを含む入力可能なPDFがあります。

チェックボックス名とその値を取得するにはどうすればよいですか?また、チェックボックス/ラジオボタンであることをどのように知ることができますか?

私は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

ラジオボタン、チェックボックス、およびボタンはすべて実際には同じタイプのフィールドですが、異なるフラグが設定されています。フラグは、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通常、これは1つだけですが、それ以上になることもあるため、これを考慮する必要があります。

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
}

以下は、iTextSharp 5.2.0をターゲットとする完全に機能するC#WinForm 2011アプリであり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();
        }
    }
}
于 2012-05-18T20:53:03.773 に答える
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 に答える
0

ラジオボタンにSystem.Linqが含まれているC#、ラジオ形式のすべてのオプションから選択されているオプションを取得し、AdobeAcrobatProで指定された名前ですべての選択オプションも印刷します

  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 に答える