iText pdf library
Website search

How to find the absolute position and dimension of a field?

Given an field name, is it possible to find the absolute position and dimension of that particular field (getLeft(), getTop(), getWidth(), getHeight())?

And is the opposite possible: if I know the position, can I get the name of the field?

Posted on StackOverflow on Apr 16, 2014 by ahairshi

First part of your question:

Suppose that you have a PdfAcroForm instance (form), retrieved from a PdfDocument in iText 7:

PdfDocument pdfDoc = new PdfDocument(new PdfReader(src));
PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);

Then you can get the field position of the first widget that corresponds with a specific field name like this:

PdfArray position = form.getField(name).getWidgets().get(0).getRectangle();

Note that one field can correspond with multiple widgets. For instance, to get the second widget, you need:

PdfArray position = form.getField(name).getWidgets().get(1).getRectangle();

The position value will consist of the lower-left and upper-right x and y coordinates: [llx, lly, urx, ury].

If you want to know some information about the page, try this:

PdfPage page = form.getField(name).getWidgets().get(0).getPage();

Second part of your question

Fields correspond with widget annotations. If you know the page number of the widget annotation, you could get the page dictionary and inspect the entries of the /Annots array. You'll have to loop over the different annotations, inspecting each annotation's /Rect entry. Once you find a match, you need to crawl the content for the field that corresponds with the annotation. That's more work than can be provided in a code sample on this site.

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