迭代pdf c#和itextsharp的复选框

时间:2018-01-16 20:03:02

标签: c# pdf foreach itext

我有一个包含多个文本框的表单,需要获取这些文本的值并通过itextsharp将它们插入到PDF中。目前我对每个人使用if语句,

If CheckBox1.Checked Then
        pdfFormFields.SetField("CheckBox1", "Yes")
    Else
        pdfFormFields.SetField("CheckBox1", "No")
    End If

我希望对表单中的所有复选框执行相同的操作,但希望功能可以执行此操作,我尝试了几种方法,但似乎无法找到有效的方法。

我可以用

foreach(Control c in this.Controls)
{
   if(c is CheckBox)
   {
   // Do stuff here ;]
   }
}

迭代它们,但它的检查状态和预先形成的行动即将停留在

1 个答案:

答案 0 :(得分:0)

假设复选框名称在表单中相同,而PDF则无法执行:

foreach(var checkbox in this.Controls.OfType<CheckBox>())
{
   pdfFormFields.SetField(checkbox.Name, checkbox.Checked ? "Yes": "No");
}