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:

fields.get("text_2").setFieldFlags(0);
fields.get("text_2").setBorderColor(Color.RED);
fields.get("text_2").setValue("bruno");

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 that the border color (/BC) entry of the /MK entry is an array with three values: [ 1 0 0 ]. This means that the border color is an RGB color where the value for Red is 1, the value for Green is 0, and the value for Blue is 0. This is consistent with us setting the color to Color.RED when we created the file.

You say that you have the AcroField.Item object for a field. In iText 7 it would be a PdfFormField object. Now you need to get the widget annotation dictionary and follow the path to the /BC dictionary. This will look like this (name is a String value of the field you are interested in):

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
PdfDictionary field = form.getField(name).getWidgets().get(0).getPdfObject();
PdfDictionary mk = field.getAsDictionary(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 5.

JavaScript errors detected

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

If this problem persists, please contact our support.