Retrieving Form Field Locations
Retrieving the X and Y coordinates of a form field is useful if you wish to use a form field as a placeholder on the PDF document. For instance, you may want to place a form field on a PDF template so that the location of that form field could later be used to place an image, barcode, etc. on the final output document.
The following example demonstrates reading the X and Y locations of two form fields and then placing a rectangle on the form using the dimensions from the form fields.
PdfDocument pdfDocument = new PdfDocument("form-example.pdf");
MergeDocument document = new MergeDocument("form-example.pdf");
Page page = document.Pages[0];
float x = pdfDocument.Form.Fields["check_box_nm"].GetX(page);
float y = pdfDocument.Form.Fields["check_box_nm"].GetY(page);
float y2 = pdfDocument.Form.Fields["text_field_name"].GetY(page) + pdfDocument.Form.Fields["text_field_name"].Height;
Rectangle rec = new Rectangle(x, y, 200, y2 - y);
rec.FillColor = RgbColor.LightBlue;
rec.BorderColor = RgbColor.Navy;
page.Elements.Add(rec);
document.Draw(outputPath);
Dim pdfDocument As New PdfDocument("form-example.pdf")
Dim document As New MergeDocument("form-example.pdf")
Dim page As Page = document.Pages(0)
Dim x As Single = pdfDocument.Form.Fields("check_box_nm").GetX(page)
Dim y As Single = pdfDocument.Form.Fields("check_box_nm").GetY(page)
Dim y2 As Single = pdfDocument.Form.Fields("text_field_name").GetY(page) + pdfDocument.Form.Fields("text_field_name").Height
Dim rec As New Rectangle(x, y, 200, y2 - y)
rec.FillColor = RgbColor.LightBlue
rec.BorderColor = RgbColor.Navy
page.Elements.Add(rec)
document.Draw(outputPath)
Figure 1. Retrieving form fields