ラジオボタン、チェックボックス、およびボタンはすべて実際には同じタイプのフィールドですが、異なるフラグが設定されています。フラグは、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();
}
}
}