简体   繁体   English

如何使用PDFBOX为PDF表单中存在的每个字段设置定义值

[英]How to set defined value for each field present in PDF Form using PDFBOX

How to set defined value for each and every field present in PDF Form, Consider that I have 5 fields in my PDF form say 2 Text box (First Name and Last Name) two Check box (Check_1,Check_2) , 2 Radio Button(Male,Female) and then at last i have another Text box (Address) , Now I have to define or set a value each and every field say 1 for First Name and 2 for Last Name , 3 for Check_1 and goes on till 7 for Address Below is the piece of code to define the value for each field but I'm facing some issue while setting up value for Radio button fields when it comes to Male and Female field it makes displays "1" for male and female for other fields it displays the correct value 如何为PDF表单中的每个字段设置定义值,请考虑一下我的PDF表单中有5个字段,例如2文本框(名和姓),两个复选框(Check_1,Check_2),2个单选按钮(男) ,女性),最后我有另一个文本框(地址),现在我必须定义或设置一个值,每个字段说1表示姓氏,2表示姓氏,3表示Check_1,然后一直到7表示Address下面是一段代码,用于定义每个字段的值,但是当涉及到“男性”和“女性”字段时,我为单选按钮字段设置值时遇到了一些问题,它使其他字段的“男性”和“女性”显示为“ 1”显示正确的值

Can any one help me out please ... 有人可以帮我吗...

Thanks 谢谢

List FieldTypes = form.getFields();
    PDField pdfFields;
        for (int i = 0; i < FieldTypes.size(); i++) {

            pdfFields = (PDField) FieldTypes.get(i);
            String type = null;

            if (pdfFields instanceof PDTextbox) {
                type = "TextBox";
                String iAsString = Integer.toString(i);
                pdfFields.setValue(iAsString);
                System.out.println("Text" + "   "+ pdfFields.getFullyQualifiedName());
             } else if (pdfFields instanceof PDCheckbox) {
                type = "CheckBox";
                String iAsString = Integer.toString(i);
                System.out.println(iAsString);
                System.out.println("CheckBox" + "   "+ pdfFields.getFullyQualifiedName());
            } else if (pdfFields instanceof PDRadioCollection) {

                List kids = pdfFields.getKids();
                for (Object kid : kids) {
                      if (kid instanceof PDCheckbox) {
                      PDCheckbox checkbox = (PDCheckbox) kid;
                      String Name = checkbox.getOnValue();
                      String iAsString = Integer.toString(i);
                      System.out.println(iAsString);
                      type = "RadioButton";
                      System.out.println("RadioButton"+"  "+checkbox.getOnValue());
                      }

                }
            }
        }

Based on doc i think getOnValue() method returns only the status like checked or not checked. 基于文档,我认为getOnValue()方法仅返回已检查或未检查的状态。 Can you try getPartialName(),getValue(),getFullyQualifiedName(),getAlternateFieldName() or any other methods. 您可以尝试getPartialName(),getValue(),getFullyQualifiedName(),getAlternateFieldName()还是任何其他方法。

With Radio Buttons you have to figure out what the "On Values" are, similar to a Checkbox. 使用单选按钮,您必须弄清楚“按值”是什么,类似于复选框。 However there are multiple On values for Radio Buttons, so its a different method. 但是,单选按钮有多个On值,因此它是一种不同的方法。

For example, if I run this against a Radio Button group where you have to choose from Checking Savings or Money Market, we would see this printed: 例如,如果我对单选按钮组运行此命令,则必须从“检查储蓄”或“货币市场”中进行选择,我们将看到以下内容:

Type of Account (org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton) 帐户类型(org.apache.pdfbox.pdmodel.interactive.form.PDRadioButton)

Radio Button 单选按钮

[Savings, Checking, Money Market] [储蓄,检查,货币市场]

public void list(PDField field)
{
    System.out.println(field.getFullyQualifiedName() + " (" + field.getClass().getName() +")");

    if(field instanceof PDRadioButton){
        System.out.println("Radio Button");
        System.out.println(((PDRadioButton) field).getOnValues());
    }
}

If I wanted to have the "Checking" button checked, I would set the field's value equal to "Checking". 如果我想选中“检查”按钮,则将字段值设置为“检查”。

Hopefully this helps. 希望这会有所帮助。

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

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