How to find the page a known Field is on?


If I know the name of a Field, how do I determine the page it is on?


The simple answer is use the following.

// Not recommended approach
Field field = doc.getField(fieldName);
Widget widget = field.getSDFObj();
Page page = widget.getPage();
int pageNum = page.getIndex(); // could throw here.

However, fields can actually be visible on more than one page, and also the link from the annotation, to the page can be incorrect.

To clarify, Fields are technically not part of any page, instead Widget Annotations are the visual representation of a field, and annotations are part of a page. This means a Field can actually be visible in multiple locations, and multiple pages even.

Finally, Annotations are supposed to have a link to the page they are on, but this is often buggy. The Pages own array of annotations is the canonical way to determine if an annotation is on a page or not.

So the best way to determine which page(s) a Field is on is the following code.

for (PageIterator itr = doc.getPageIterator(); itr.hasNext(); )
	Page page =;
	int num_annots = page.getNumAnnots();
	for (int i = 0; i < num_annots; ++i)
		Annot annot = page.getAnnot(i);
		if (annot.isValid() == false) continue;
		if(annot.getType() != Annot.e_Widget) continue;
		Widget widget = new Widget(annot);
		Field field = widget.getField();
		String fieldName = field.getName();