How can I find the number of the page that a specific text field is on?

Q:

I have a PDF document, and on one of its pages is a field with the name “FOO”. How can I find the page number of that page?

A:

First you will need to traverse the document’s annotations, as in the following code sample:

Then, you will need to modify the sample to have it check text field names, like so:

if (annot.GetType() == Annot.Type.e_Widget) {
Field field = annot.GetWidgetField();
string fieldName = field.GetName();
// check if fieldName is “FOO”…
}

If you’re following the Annotation sample, the page_num variable holds the current page number.

Hi, Could you please provide the code sample for this, preferably in JavaScript?
I’m trying to get the page numbers for specific Header Texts in PDF to construct my own ToC.