How can I obtain a rectangle for a form field?

Q: I am writing in C#. Given a Field, obtained by iterating over all
fields in a PDF, can I find its rectangle (bounding box)? I want to
add an annotation near it, and Annot’s constructor says it is
A: You could enumerate all annotations on a given page (e.g. as shown
in Annotation sample project -
Once you encounter an annotation of type Annot.Type.e_Widget you can
get the bounding box for the field using annot.GetRect(). Also you
get a field from the annotation using annot.GetWidgetField().

This is further explained in the following KB articles:


Another approach is to convert a field to annotation. The only
complication is that in PDF a form field may not have a visible
annotation. The following C# pseudocode will convert a form field to a
widget annotation:

// In C#
Obj fld_dict = field.GetSDFObj();
if (fld_dict.FindObj("Rect") != null) {
  Annot widget = new Annot(fld_dict);
  Rect rect = widget.GetRect();

// In C++
Obj fld_dict = field.GetSDFObj();
if (fld_dict.FindObj("Rect")) {
  Annot widget(fld_dict);
  Rect rect = widget.GetRect();