How to find the position and dimensions 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 dimensions of PDF form fields using C# and VB.NET.
Steps to find the position and dimensions of PDF form fields programmatically:
- Create a new C# Windows Forms application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a 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)
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.
Note:
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from the trial setup or the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering the Syncfusion® license key in your application to use the components without a trial message.
Conclusion:
I hope you enjoyed learning about how to find the position and dimensions of PDF form fields using C# and VB.NET.
You can refer to our Flutter PDF feature tour page to learn about its other groundbreaking features and documentation, and how to quickly get started with configuration specifications. You can also explore our Flutter PDF Flutter PDF examples 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 explore 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 or feedback portal. We are always happy to assist you!