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 how to read the X and Y location of a form field and then places an image at that same location. This example also pulls the Height and Width of the form field so that those values can be used when placing the image on the PDF:
PdfDocument pdfDocument = new PdfDocument(pdfFilePath);
MergeDocument document = new MergeDocument(pdfDocument, MergeOptions.None);
Page page = document.Pages[0];
Image image = new Image"image.gif", 0, 0);
image.X = pdfDocument.Form.Fields["PlaceHolderField"].GetX(page);
image.Y = pdfDocument.Form.Fields["PlaceHolderField"].GetY(page);
image.Height = pdfDocument.Form.Fields["PlaceHolderField"].Height;
image.Width = pdfDocument.Form.Fields["PlaceHolderField"].Width;
page.Elements.Add(image);
document.Draw(pdfFilePath);
Dim MyPdfDocument as PdfDocument = New PdfDocument(pdfFilePath)
Dim MyDocument As MergeDocument = New MergeDocument(MyPdfDocument, MergeOptions.None)
Dim MyPage as Page = MyDocument.Pages(0)
Dim MyImage As Image = New Image("image.gif", 0, 0)
MyImage.X = MyPdfDocument.Form.Fields("PlaceHolderField").GetX(MyPage)
MyImage.Y = MyPdfDocument.Form.Fields("PlaceHolderField").GetY(MyPage)
MyImage.Height = MyPdfDocument.Form.Fields("PlaceHolderField").Height
MyImage.Width = MyPdfDocument.Form.Fields("PlaceHolderField").Width
MyPage.Elements.Add(MyImage)
MyDocument.Draw(pdfFilePath)