Skip to main content
Skip table of contents

How can I find the border color of a field?

Is there anyway of finding the border color of a specific field in my PDF? I could get AcroField.Item, but I don't see an option to get border color from there.

Posted on StackOverflow on Feb 4, 2015 by user2296988

Please take a look at the TextFields example. You'll find the following code snippet to set the border of the field with name text_2:

text.setBorderStyle(PdfBorderDictionary.STYLE_SOLID);
text.setBorderColor(BaseColor.BLUE);
text.setBorderWidth(2);

Now when we look inside the resulting PDF using iText RUPS, and we take a look at the field dictionary / widget annotation for this field, we see the following structure:

Internal structure of a PDF file with fields

Internal structure of a PDF file with fields

We see a /BS dictionary that defines a solid border style (the value for the /S key is /S) and a border width (/W) with value 2.

We also see that the border color (/BC) entry of the /MK entry is an array with three values: [ 0 0 1 ]. This means that the border color is an RGB color where the value for Red is 0, the value for Green is 0, and the value for Blue is 1. This is consistent with us setting the color to BaseColor.BLUE when we created the file.

You say that you have the AcroField.Item object for a field. Now you need to get the merged field / widget annotation dictionary and follow the path shown by iText RUPS:

AcroFields.Item item = acroFields.getFieldItem(fldName);
PdfDictionary merged = item.getMerged(0);
PdfDictionary mk = merged.getAsDict(PdfName.MK);
PdfArray bc = mk.getAsArray(PdfName.BC);

The values stored in the array bc will inform you about the background color. If the array has only one value, you have a gray color, if there are three, you have an RGB color, if there are four, you have a CMYK color.

Warning: some values may not be present (e.g. there may be no /BC entry). In that case you can get NullPointerExceptions.

Click this link if you want to see how to answer this question in iText 7.

JavaScript errors detected

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

If this problem persists, please contact our support.