如何使用iText获取AcroField属性?

时间:2022-09-25 21:09:17

I am using iText to read a pdf file. I have 20 Acro text fields in my pdf with different fillColor properties. I tried to read the properties but could not able to read. Is there any way we can get the fields which have a particular color as their fillcolor property in java?

我正在使用iText来阅读pdf文件。我的pdf中有20个Acro文本字段,具有不同的fillColor属性。我试图阅读属性但无法阅读。有没有什么办法可以让我们在java中获得具有特定颜色的字段作为fillcolor属性?

EDIT: I created acro fields in the pdf using the following Adobe Javascript

编辑:我使用以下Adobe Javascript在pdf中创建了acro字段

   var oFld = this.addField("nameOfField", "button", 0, fldRect);
    if (oFld != null) {
        oFld.buttonSetCaption("");
        oFld.borderStyle = border.s;
        oFld.fillColor = color.gray;
        oFld.textColor = color.white;
        oFld.lineWidth = 1;
    }

UPDATE:

更新:

now I am able to read the color properties.

现在我能够读取颜色属性。

Am not sure whether it is the right way to do it or not?

我不确定这是否是正确的做法?

  if(dict.getAsDict(PdfName.MK).get(PdfName.BG) !=null){
            if(dict.getAsDict(PdfName.MK).get(PdfName.BG).toString().trim().equals("     
    [0.0, 1.0, 0.0]")){
                System.out.println(entry.getKey());
                System.out.println("Green");
            }
            if(dict.getAsDict(PdfName.MK).get(PdfName.BG).toString().trim().equals("
        [1.0, 0.0, 0.0]")){
                System.out.println(entry.getKey());
                System.out.println("Red");
            }

        }

1 个解决方案

#1


5  

Chapter 8 of my book discusses AcroForm fields from a rather high level. If you want to dig deeper, you need chapter 13. On page 449, table 13.11 lists the different AcroFields.Item methods. As you know a form field is described using a form dictionary. The visual representation(s) of a field is (or are) described using one or more widget annotations. You're looking for a property of the appearance, so you need the annotation dictionary.

我的书的第8章从较高的层面讨论了AcroForm字段。如果您想深入挖掘,则需要第13章。在第449页上,表13.11列出了不同的AcroFields.Item方法。如您所知,使用表单字典描述表单字段。使用一个或多个窗口小部件注释来描述(或者)描述字段的视觉表示。您正在寻找外观的属性,因此您需要注释字典。

You also know that the field dictionary and the widget dictionary are often merged when one field corresponds with one widget annotation, and that's why the AcroFields.Item class has a method called getMerged(). For every widget annotation of a specific field, it returns the merged properties of the field and the widget annotation.

您还知道,当一个字段对应一个窗口小部件注释时,通常会合并字段字典和窗口小部件字典,这就是AcroFields.Item类有一个名为getMerged()的方法的原因。对于特定字段的每个窗口小部件注释,它返回字段和窗口小部件注释的合并属性。

That's the theory. Let's look at an example: InspectForm

这就是理论。让我们看一个例子:InspectForm

Map<String,AcroFields.Item> fields = form.getFields();
AcroFields.Item item;
PdfDictionary dict;
int flags;
for (Map.Entry<String,AcroFields.Item> entry : fields.entrySet()) {
    out.write(entry.getKey());
    item = entry.getValue();
    dict = item.getMerged(0);
    // inspect dict
}

In the example, we inspect the field flags (/FF), which are properties of the field dictionary. You are interested in appearance characteristics, so I guess you'll want to inspect the /MK entry, which is (ISO-32000-1 Table 188):

在该示例中,我们检查字段标志(/ FF),它们是字段字典的属性。你对外观特征感兴趣,所以我想你要检查/ MK条目,即(ISO-32000-1表188):

An appearance characteristics dictionary (see Table 189) that shall be used in constructing a dynamic appearance stream specifying the annotation’s visual presentation on the page. The name MK for this entry is of historical significance only and has no direct meaning.

外观特征字典(参见表189),用于构造指定页面上注释的可视化表示的动态外观流。此条目的名称MK仅具有历史意义,没有直接含义。

You'll need to look at table 189 to find out the specific attributes you want:

您需要查看表189以找出所需的特定属性:

R integer (Optional): The number of degrees by which the widget annotation shall be rotated counterclockwise relative to the page. The value shall be a multiple of 90. Default value: 0.

R integer(可选):窗口小部件注释相对于页面逆时针旋转的度数。该值应为90的倍数。默认值:0。

BC array (Optional): An array of numbers that shall be in the range 0.0 to 1.0 specifying the colour of the widget annotation’s border. The number of array elements determines the colour space in which the colour shall be defined: 0 No colour; transparent 1 DeviceGray 3 DeviceRGB 4 DeviceCMYK

BC数组(可选):一个数字数组,其范围应为0.0到1.0,用于指定窗口小部件注释边框的颜色。数组元素的数量决定了颜色应该定义的颜色空间:0没有颜色;透明1 DeviceGray 3 DeviceRGB 4 DeviceCMYK

BG array (Optional): An array of numbers that shall be in the range 0.0 to 1.0 specifying the colour of the widget annotation’s background. The number of array elements shall determine the colour space, as described for BC.

BG数组(可选):一个数字数组,其范围应为0.0到1.0,用于指定窗口小部件注释背景的颜色。数组元素的数量应确定颜色空间,如BC所述。

CA text string (Optional; button fields only): The widget annotation’s normal caption, which shall be displayed when it is not interacting with the user. Unlike the remaining entries listed in this Table, which apply only to widget annotations associated with pushbutton fields (see Pushbuttons in 12.7.4.2, “Button Fields”), the CA entry may be used with any type of button field, including check boxes (see Check Boxes in 12.7.4.2, “Button Fields”) and radio buttons (Radio Buttons in 12.7.4.2, “Button Fields”).

CA文本字符串(可选;仅限按钮字段):窗口小部件注释的常规标题,在不与用户交互时显示。与此表中列出的仅适用于与按钮字段关联的窗口小部件注释的其余条目(请参阅12.7.4.2中的按钮,“按钮字段”)不同,CA条目可以与任何类型的按钮字段一起使用,包括复选框(请参阅12.7.4.2,“按钮字段”)和单选按钮(12.7.4.2中的单选按钮,“按钮字段”)中的复选框。

RC text string (Optional; pushbutton fields only): The widget annotation’s rollover caption, which shall be displayed when the user rolls the cursor into its active area without pressing the mouse button.

RC文本字符串(可选;仅限按钮字段):窗口小部件注释的翻转标题,当用户将光标滚动到其活动区域而不按下鼠标按钮时,将显示该标题。

AC text string (Optional; pushbutton fields only): The widget annotation’s alternate (down) caption, which shall be displayed when the mouse button is pressed within its active area.

AC文本字符串(可选;仅限按钮字段):窗口小部件注释的备用(向下)标题,在其活动区域中按下鼠标按钮时将显示该标题。

When you ask for the fill color, I assume that you're referring to the background color, which means you'll have to look at the BC entry for the colorspace, and at the BG entry for the actual color value.

当你要求填充颜色时,我假设你指的是背景颜色,这意味着你必须查看颜色空间的BC条目,以及实际颜色值的BG条目。

#1


5  

Chapter 8 of my book discusses AcroForm fields from a rather high level. If you want to dig deeper, you need chapter 13. On page 449, table 13.11 lists the different AcroFields.Item methods. As you know a form field is described using a form dictionary. The visual representation(s) of a field is (or are) described using one or more widget annotations. You're looking for a property of the appearance, so you need the annotation dictionary.

我的书的第8章从较高的层面讨论了AcroForm字段。如果您想深入挖掘,则需要第13章。在第449页上,表13.11列出了不同的AcroFields.Item方法。如您所知,使用表单字典描述表单字段。使用一个或多个窗口小部件注释来描述(或者)描述字段的视觉表示。您正在寻找外观的属性,因此您需要注释字典。

You also know that the field dictionary and the widget dictionary are often merged when one field corresponds with one widget annotation, and that's why the AcroFields.Item class has a method called getMerged(). For every widget annotation of a specific field, it returns the merged properties of the field and the widget annotation.

您还知道,当一个字段对应一个窗口小部件注释时,通常会合并字段字典和窗口小部件字典,这就是AcroFields.Item类有一个名为getMerged()的方法的原因。对于特定字段的每个窗口小部件注释,它返回字段和窗口小部件注释的合并属性。

That's the theory. Let's look at an example: InspectForm

这就是理论。让我们看一个例子:InspectForm

Map<String,AcroFields.Item> fields = form.getFields();
AcroFields.Item item;
PdfDictionary dict;
int flags;
for (Map.Entry<String,AcroFields.Item> entry : fields.entrySet()) {
    out.write(entry.getKey());
    item = entry.getValue();
    dict = item.getMerged(0);
    // inspect dict
}

In the example, we inspect the field flags (/FF), which are properties of the field dictionary. You are interested in appearance characteristics, so I guess you'll want to inspect the /MK entry, which is (ISO-32000-1 Table 188):

在该示例中,我们检查字段标志(/ FF),它们是字段字典的属性。你对外观特征感兴趣,所以我想你要检查/ MK条目,即(ISO-32000-1表188):

An appearance characteristics dictionary (see Table 189) that shall be used in constructing a dynamic appearance stream specifying the annotation’s visual presentation on the page. The name MK for this entry is of historical significance only and has no direct meaning.

外观特征字典(参见表189),用于构造指定页面上注释的可视化表示的动态外观流。此条目的名称MK仅具有历史意义,没有直接含义。

You'll need to look at table 189 to find out the specific attributes you want:

您需要查看表189以找出所需的特定属性:

R integer (Optional): The number of degrees by which the widget annotation shall be rotated counterclockwise relative to the page. The value shall be a multiple of 90. Default value: 0.

R integer(可选):窗口小部件注释相对于页面逆时针旋转的度数。该值应为90的倍数。默认值:0。

BC array (Optional): An array of numbers that shall be in the range 0.0 to 1.0 specifying the colour of the widget annotation’s border. The number of array elements determines the colour space in which the colour shall be defined: 0 No colour; transparent 1 DeviceGray 3 DeviceRGB 4 DeviceCMYK

BC数组(可选):一个数字数组,其范围应为0.0到1.0,用于指定窗口小部件注释边框的颜色。数组元素的数量决定了颜色应该定义的颜色空间:0没有颜色;透明1 DeviceGray 3 DeviceRGB 4 DeviceCMYK

BG array (Optional): An array of numbers that shall be in the range 0.0 to 1.0 specifying the colour of the widget annotation’s background. The number of array elements shall determine the colour space, as described for BC.

BG数组(可选):一个数字数组,其范围应为0.0到1.0,用于指定窗口小部件注释背景的颜色。数组元素的数量应确定颜色空间,如BC所述。

CA text string (Optional; button fields only): The widget annotation’s normal caption, which shall be displayed when it is not interacting with the user. Unlike the remaining entries listed in this Table, which apply only to widget annotations associated with pushbutton fields (see Pushbuttons in 12.7.4.2, “Button Fields”), the CA entry may be used with any type of button field, including check boxes (see Check Boxes in 12.7.4.2, “Button Fields”) and radio buttons (Radio Buttons in 12.7.4.2, “Button Fields”).

CA文本字符串(可选;仅限按钮字段):窗口小部件注释的常规标题,在不与用户交互时显示。与此表中列出的仅适用于与按钮字段关联的窗口小部件注释的其余条目(请参阅12.7.4.2中的按钮,“按钮字段”)不同,CA条目可以与任何类型的按钮字段一起使用,包括复选框(请参阅12.7.4.2,“按钮字段”)和单选按钮(12.7.4.2中的单选按钮,“按钮字段”)中的复选框。

RC text string (Optional; pushbutton fields only): The widget annotation’s rollover caption, which shall be displayed when the user rolls the cursor into its active area without pressing the mouse button.

RC文本字符串(可选;仅限按钮字段):窗口小部件注释的翻转标题,当用户将光标滚动到其活动区域而不按下鼠标按钮时,将显示该标题。

AC text string (Optional; pushbutton fields only): The widget annotation’s alternate (down) caption, which shall be displayed when the mouse button is pressed within its active area.

AC文本字符串(可选;仅限按钮字段):窗口小部件注释的备用(向下)标题,在其活动区域中按下鼠标按钮时将显示该标题。

When you ask for the fill color, I assume that you're referring to the background color, which means you'll have to look at the BC entry for the colorspace, and at the BG entry for the actual color value.

当你要求填充颜色时,我假设你指的是背景颜色,这意味着你必须查看颜色空间的BC条目,以及实际颜色值的BG条目。