如何使用PDFbox(C#)从PDF表单中获取单选按钮值
在Java中:
doc = PDDocument.load("C:\\Users\\347702\\Desktop\\sample_form.pdf");
List pages = doc.getDocumentCatalog().getAllPages();
PDAcroForm form = doc.getDocumentCatalog().getAcroForm();
List FieldTypes = form.getFields();
PDField pdfFields;
for (int i = 0; i < FieldTypes.size(); i++) {
pdfFields = (PDField) ((List) FieldTypes).get(i);
if (pdfFields instanceof PDRadioCollection)
{
String iAsString = Integer.toString(k);
System.out.println(iAsString);
type = "RadioButton";
System.out.println("Radio Button" + " "
+ pdfFields.getFullyQualifiedName() + " ");
List kids = pdfFields.getKids();
for (Object kid : kids) {
if (kid instanceof PDCheckbox) {
PDCheckbox checkbox = (PDCheckbox) kid;
String Name = checkbox.getOnValue();
System.out.println(Name);
}
}
上面的java代码已成功运行(成功返回Radiobuttons的子代)但是当在c#中实现相同的逻辑时,它没有按预期运行
下面是我的c#代码:
else if (pdfFields is PDRadioCollection)
{
System.Console.WriteLine("Radio Button" + " "
+ pdfFields.getFullyQualifiedName() + " ");
List kids = pdfFields.getKids();
System.Console.WriteLine(kids);
for (int w = 0; w < kids.size(); w++)
{
kids.get(w);
System.Console.WriteLine(kids.get(w)); ---- > Return null
System.Console.WriteLine(kids.get(w).ToString()); -- > Return null
}
}
请帮忙 谢谢
答案 0 :(得分:0)
最后得到了它的工作:这里是:: C#:需要用IEnumerable键入case:
else if (pdfFields is PDRadioCollection)
{
System.Console.WriteLine("Radio Button" + " "
+ pdfFields.getFullyQualifiedName() + " ");
System.Collections.IEnumerable kids = (System.Collections.IEnumerable)pdfFields.getKids();
System.Console.WriteLine(kids);
foreach (object kid in kids)
{
PDCheckbox checkbox = (PDCheckbox)kid;
checkbox.check();
}