How to get the color properties of an AcroForm field?

Tags: formsInspect fieldsfield propertiesfield colorcolorbackground colorfield borderiText 5

I am using iText to read a PDF file. I have 20 AcroForm text fields in my pdf with different color fill properties. I'm not able to read these properties. Is there any way we can get color information from the fields?

EDIT: I created the AcroForm 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;
}
Posted on StackOverflow on Nov 25, 2013 by MaheshVarma

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.

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.

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
}

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

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.

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

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

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.