2012-05-18 42 views
3

我有一個可填寫的PDF包含CheckBoxes和RadioButtons和TextBox。使用iText Sharp閱讀複選框,單選按鈕的名稱和值PDF格式

如何獲取CheckBox名稱及其值我們如何知道它是一個複選框/單選按鈕?

我使用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

下面的代碼可能會幫助你,如果你仍然需要它。它僅適用於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是它的價值。

對於複選框,如果(field.Value ==「Yes」),那麼它被選中...如果它是別的東西,它不會被選中。

編輯:

而且我剛剛發現的get單選按鈕如何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); 
+0

感謝讓我試試它... –

+0

它爲你工作? – Mike

+0

sry ..需要chk on ...喲知道它是在一個月前發佈的... m busy wid sm其他工作...當然我會在我最早的時候確認喲...感謝你的努力夥伴:) –

3

單選按鈕,複選框和按鈕都實際上是同一類型的字段,但與設置不同的標誌。你可以看到國旗在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 

對於要獲得與此相關的Widget個給出Field。通常這只是一個,但可以更多,所以你應該解釋這一點。

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

鍵字段將具有設置爲/Btn他們的字段類型(/Ft),以便檢查該

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 
} 

下面是一個完整的工作C#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(); 
     } 
    } 
} 
0

C#與System.Linq的包括 單選按鈕,得到的選項是從所有無線電的形式,打印也都選擇的選項由他們指定的名稱在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 + " "); 
} 
} 
}