0

如何使用 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
                   }  

                } 

有什么可以帮忙的吗谢谢

4

1 回答 1

0

终于让它工作了:这里是 :: C#:需要用 IEnumerable 输入大小写:

                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();
                    }
于 2013-02-18T11:17:56.863 回答