如何使用iText获取AcroField属性? [英] How to get AcroField Properties using iText?

查看:332
本文介绍了如何使用iText获取AcroField属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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

   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;
    }

更新:

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

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");
            }

        }


推荐答案

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

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.

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

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.

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

That's the theory. Let's look at an example: 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
}

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

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):


一个外观特征字典(见表189),它应该是
,用于构造动态外观流,指定
注释的视觉呈现在页面上。此
条目的名称MK仅具有历史意义,并没有直接的含义。

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中找出你想要的具体属性:

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


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

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.

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

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

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

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.

CA 文本字符串(可选;仅限按钮字段):
窗口小部件注释的常规标题,当
未与用户交互时,将显示该标题。与此表中列出的剩余条目
不同,它仅适用于与
按钮字段关联的小部件注释(请参阅12.7.4.2中的按钮,按钮字段),
CA条目可能是用于任何类型的按钮字段,包括检查
框(参见12.7.4.2中的复选框,按钮字段)和单选按钮
(12.7.4.2中的单选按钮,按钮字段)。

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").

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

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.

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

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.

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

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.

这篇关于如何使用iText获取AcroField属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆