Q: How do I determine the position and size of a form field on a
page?
I've looked through the FAQs and samples but cannot find any method to
determine the position and size of a form field on a page.
------
A: A form field is just another type of annotation. As a result, to
get the positioning for the form filed you can use Annot.GetRect().
For example,
int annot_num = page.GetNumAnnots();
for (int i=0; i<annot_num; ++i) {
Annot annot = page.GetAnnot(i);
Rect box = annot.GetRect();
...
// Process only widget (i.e. form field) annotations...
if (annot.IsValid() == false || annot.GetType() !=
Annot.Type.e_Widget) continue;
if (box.Contains(x, y)) { ...
}
}
Q: Thanks, that’s what I was looking for. Am I correct in assuming the
returned rectangle is in points and is an offset from the origin (the
upper left hand point of the page)?
A: The rectangle is expressed in PDF points (1pt = 1/72 inch) and is
relative to the PDF user coordinate system (which starts at the lower-
left corrdinate of the page - in most cases). One thing to keep in
mind is that if the page is rotated/cropped etc you may want to
transform the bounding box in the rotated/cropped page coordinate
system as follows:
Matrix2D pg_mtx = page.GetDefaultMatrix();
…
int annot_num = page.GetNumAnnots();
for (int i=0; i<annot_num; ++i) {
Annot annot = page.GetAnnot(i);
Rect box = annot.GetRect();
pg_mtx.Mult(bbox.x1, bbox.y1);
pg_mtx.Mult(bbox.x2, bbox.y2);
… use annotation bbox …
}