Getting a Page Number Based on a Field?

Product: PDFTron .NET x64

Product Version: 9.1.0

Getting a Page Number Based on a Field?

I am currently using the PDFTron SDK to set form values based on some data and a field mapping schema provided in XML. This requires me to loop through the XML data nodes, retrieve the corresponding mapping value, and then retrieve the corresponding Field from the PDF document in order to set its value.

We currently have a “cursor” sign widget that allows the user to draw their signature, which is then saved as a PNG image. This widget is on an HTML page from which data must be transferred/converted to PDF.

I’m using the Stamper class to stamp this image onto the middle of the signature field. I can get all the relevant position data from Field.GetUpdateRect(), but I can’t figure out an easy and efficient way to get the page number for a given field.

The only code I’ve been able to cobble together requires looping through every control on every page until I find a match for the field name (see below). Is there an easier and more efficient way to do this, given that I’ve already retrieved the Field object?

        private int GetPageNumberFromFieldName(string fieldName, PDFDoc srcDoc) {
            int pageNum = -1;
            int pageCount = srcDoc.GetPageCount();
            for (int pageNo = 1; pageNo <= pageCount; pageNo++) {
                pdftron.PDF.Page page = srcDoc.GetPage(pageNo);
                if (page != null) {
                    //Get number of controls on the page
                    int fieldCount = page.GetNumAnnots();

                    //loop through all those fields...
                    for (int loop = 0; loop < fieldCount; loop++) {
                        Annot annot = page.GetAnnot(loop);

                        if (!annot.IsValid()) {
                            continue;
                        }

                        //if it is valid widget control
                        if (annot.GetType() == Annot.Type.e_Widget) {
                            pdftron.PDF.Annots.Widget w = new
pdftron.PDF.Annots.Widget(annot);
                            Field field = w.GetField();

                            if (field != null && field.GetName() == fieldName) {
                                return pageNo;
                            }
                        }
                    }
                }
            }
            return pageNum;
        }

Hello, I’m Ron, an automated tech support bot :robot:

While you wait for one of our customer support representatives to get back to you, please check out some of these documentation pages:

Guides:APIs:Forums:

Correct. Iterating the pages, then iterating for Widgets would be the correct method for getting a valid page number for a field’s annotation.

Please keep in mind that fields are not part of any page, and are represented by zero or more widget annotations.

Annotations are part of a page (if the page has been assigned).

The following post demonstrates the issue: Why do some annotations report they are on page zero?

Thank you, @JoeHecht.

My code sample didn’t make it clear that, by the time I’m trying to get a page number, I have not only the field name, but also the full Field object.

Can you confirm that, even though a field may be tied to 0 or more widget annotations, there’s no way to fetch these annotations from the Field object itself?

Hello Ted,

My answer was based on the subject of the post (“Page number Based on Field”). I still think what you cobbled together is the way to go.

The following may be helpful:

Widget widget = new Widget(field.GetSDFObj()) tends to work for annotations created with our API.

Please take a look at the following post (including both posted answers) for ideas on iterating the annotations of a field: