Skip to main content
Skip table of contents

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

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

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

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 How to find the absolute position and dimension of a 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.