Skip to main content
Skip table of contents

How to get the color properties of an AcroForm field?

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?

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

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 PdfFormField class in iText7 has a method called getWidgets(). This way to inspect widget annotations and extract the PdfDictionary you should simply use:

dict = item.getWidgets().get(0).getPdfObject();

Let's look at an example: InspectForm.

Map fields = form.getFormFields();
PdfDictionary dict;
int flags;
PdfFormField item;
for (String key : fields.keySet()) {
    out.write(key);
    item = fields.get(key);
    dict = item.getPdfObject();
    if (null == dict.getAsNumber(PdfName.Ff)) {
        // being here means that we should inspect widget annotations, (see getMerged() in itext5)
       dict = item.getWidgets().get(0).getPdfObject();
    }
    // 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.

Click How to get the color properties of an AcroForm field? if you want to see how to answer this question in iText 5.

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.