Search code examples
javaitextacrofields

How can I get the page number for a specific field in iText?


How can I get from which page is the field coming from?

PdfReader reader = new PdfReader(path);
AcroFields fields = reader.getAcroFields();
Set<String> fieldNames = fields.getFields.keySet();
for(String fieldName : fieldNames)
{
     String fieldValue = fields.getField(fieldName);
    //get page number for this field
}

Solution

  • You need the getFieldPositions() method. One field can correspond with more than one widget annotation. For instance, a field with name fieldName can be visualized on different pages, hence the method returns a List.

    So if you want to get the page of the first (or only) item, you need:

    int page = form.getFieldPositions(name).get(0).page;
    

    By the way: the coordinates can be found like this:

    Rectangle rectangle = form.getFieldPositions(name).get(0).position;