How to find the position and dimension of PDF form fields using C# and VB.NET?
Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can find the position and dimension of PDF form fields using C# and VB.NET.
Steps to find the position and dimension of PDF form fields programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.cs file.
C#
using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf Imports System.Drawing
- Use the following code snippet in the click event of button to find the position and dimension of PDF form fields.
C#
//Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Form.pdf"); //Get the loaded form PdfLoadedForm loadedForm = loadedDocument.Form; //Get the loaded form field and modify the properties PdfLoadedStyledField loadedField = loadedForm.Fields[0] as PdfLoadedStyledField; //Get bounds of the loaded form field RectangleF bounds = loadedField.Bounds; //Draw rectangle on the detected bounds loadedDocument.Pages[0].Graphics.DrawRectangle(PdfPens.Red, bounds); //Save the document loadedDocument.Save("LoadedForm.pdf"); //Close the document loadedDocument.Close(true);
VB.NET
'Load the PDF document Dim loadedDocument As New PdfLoadedDocument("Form.pdf") 'Get the loaded form Dim loadedForm As PdfLoadedForm = loadedDocument.Form 'Get the loaded form field and modify the properties Dim loadedField As PdfLoadedStyledField = TryCast(loadedForm.Fields(0), PdfLoadedStyledField) 'Get bounds of the loaded form field Dim bounds As RectangleF = loadedField.Bounds 'Draw rectangle on the detected bounds loadedDocument.Pages(0).Graphics.DrawRectangle(PdfPens.Red, bounds) 'Save the document loadedDocument.Save("LoadedForm.pdf") 'Close the document loadedDocument.Close(True)
A complete working sample can be downloaded from FindPositionOfPDFFormField.zip.
By executing the program, you will get the PDF document as follows.
Input | Output |
Take a moment to peruse the documentation for working with forms, where you will find other options like creating, removing, modifying, and filling form fields in the PDF document.
Refer here to explore the rich set of Syncfusion Essential® PDF features.
An online sample link to fill and flatten the PDF form fields.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion® license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to find the position and dimension of PDF form fields using C# and VB.NET.
You can refer to our .NET PDF
feature tour page to know about its
other groundbreaking feature representations and documentation, and how to quickly get started
for configuration specifications. You can also explore our .NET PDF
example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!