简体   繁体   English

使用iTextSharp完成PDF中的复选框

[英]Completing check boxes within a PDF using iTextSharp

I'm currently trying to take an existing PDF, find all existing check boxes and complete them based on some criteria using C#. 我目前正在尝试获取现有的PDF,找到所有现有的复选框,并使用C#根据某些条件完成这些复选框。

After reviewing other related questions: https://stackoverflow.com/a/4827996/6328714 审查其他相关问题后: https : //stackoverflow.com/a/4827996/6328714

The main issue I am having is finding all of the Checkbox objects within the PDF - which I believe I need to be able to reference the correct checkbox within my code. 我遇到的主要问题是找到PDF中的所有Checkbox对象-我相信我需要能够在代码中引用正确的复选框。

As for tools to view the PDF's internal structure I am using PDFXplorer, but I'm not having much luck finding the actual check boxes within the tree-structure. 至于查看PDF内部结构的工具,我使用的是PDFXplorer,但在树状结构中查找实际的复选框并不太幸运。

So: 所以:

  • Do I ~need~ the object to be able to check the box? 我需要物品能够选中该框吗?
  • Is checking the boxes as simple as the posted code below? 选中框是否像下面的发布代码一样简单? (It seems fairly straight forward if so) (如果这样的话,这似乎很简单)

Below example is taken from the linked question: 下面的示例摘自链接的问题:

PdfReader reader = new PdfReader(fileNameIn);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(fileNameOut));
AcroFields form = stamper.getAcroFields();

form.setField("Name","Test Name");
form.setField("odot","123456");
form.setField("Consortium","A Testing Co");
form.setField("PName","My Name");
form.setField("date","10/14/03");
form.setField("Box1","true"); //This is the checkbox control
stamper.close();

The first thing you want to do, is to find the fields that were defined for the form, and to discover which of those fields are check boxes. 您要做的第一件事是查找为表单定义的字段,并发现其中哪些是复选框。

Read this question to find out how this is done: iText doesn't set checkbox field 阅读此问题以了解如何完成此操作: iText不会设置复选框字段

public class MainClass {
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("pdf/fw9_template.pdf");
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("test.pdf"));
            AcroFields form = stamper.getAcroFields();

            String states[] = form.getAppearanceStates("topmostSubform[0].Page1[0].FederalClassification[0].c1_1");
            System.out.println(states);


            for (Iterator i = form.getFields().keySet().iterator(); i.hasNext(); ) {
                String key = (String) i.next();
                System.out.print(key + " : ");
                switch(form.getFieldType(key)) {
                    case AcroFields.FIELD_TYPE_CHECKBOX:
                        System.out.println("Checkbox");
                        break;
                    case AcroFields.FIELD_TYPE_COMBO:
                        System.out.println("Combobox");
                        break;
                    case AcroFields.FIELD_TYPE_LIST:
                        System.out.println("List");
                        break;
                    case AcroFields.FIELD_TYPE_NONE:
                        System.out.println("None");
                        break;
                    case AcroFields.FIELD_TYPE_PUSHBUTTON:
                        System.out.println("Pushbutton");
                        break;
                    case AcroFields.FIELD_TYPE_RADIOBUTTON:
                        System.out.println("Radiobutton");
                        break;
                    case AcroFields.FIELD_TYPE_SIGNATURE:
                        System.out.println("Signature");
                        break;
                    case AcroFields.FIELD_TYPE_TEXT:
                        System.out.println("Text");
                        break;
                    default:
                        System.out.println("?");
                }
            }
            form.setField("topmostSubform[0].Page1[0].FederalClassification[0].c1_1[0]", "true");

            stamper.close();
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

Then you want to check the check box, as is asked in the question Get field value for the check box 然后,您要选中复选框,如问题“ 获取复选框的字段值”中所要求的

testForm.SetField("AmountCollect", "Off");
testForm.SetField("AmountCollect", "Yes");
testForm.SetField("AmountCollect", "0");

Different check boxes can have different values ( "On" , "Yes" , "true" , "1" ,...) for the on state (read How to determine “Checked” value for Checkboxes (from GetAppearanceStates) ), so you have to use the getAppearances() method to know which values can be used. 对于打开状态,不同的复选框可以具有不同的值( "On""Yes""true""1" ,...)(请参阅如何确定Checkboxes的“ Checked”值(来自GetAppearanceStates) ),因此您必须使用getAppearances()方法来知道可以使用哪些值。

All of this is, of course, explained in great detail on the official iText web site, which is the first place you should look when you are looking for an answer: 当然,所有这些都在iText官方网站上进行了详细说明,这是您在寻找答案时应该首先查看的地方:

Browsing the official FAQ, you'll discover that iText also has a tool to inspect the objects inside a PDF. 浏览官方常见问题解答,您会发现iText还具有检查PDF内对象的工具。 That tool is called RUPS. 该工具称为RUPS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM