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:


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.

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now