How to fill form & sign a PDF file in C#, VB.NET
Syncfusion Essential PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can fill all necessary form fields and sign a PDF file with PFX file (X509 Certificate) to maintain the authenticity and integrity.
Steps to fill form fields and sign a PDF file programmatically:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as reference to your .NET Framework application from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Graphics; using Syncfusion.Pdf.Parsing; using Syncfusion.Pdf.Security; using System.Drawing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Graphics Imports Syncfusion.Pdf.Parsing Imports Syncfusion.Pdf.Security Imports System.Drawing
- Use the following code snippet to fill form and sign the PDF document.
C#
//Load the PDF document PdfLoadedDocument loadedDocument = new PdfLoadedDocument("FormTemplate.pdf"); //Gets the first page of the document PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage; //Get the loaded form PdfLoadedForm form = loadedDocument.Form; //Fill the fields from the first page (form.Fields["Name"] as PdfLoadedTextBoxField).Text = "Simons"; (form.Fields["Email address"] as PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com"; (form.Fields["Phone"] as PdfLoadedTextBoxField).Text = "31 12 34 56"; (form.Fields["Gender"] as PdfLoadedRadioButtonListField).SelectedIndex = 0; (form.Fields["JobTitle"] as PdfLoadedComboBoxField).SelectedIndex = 1; (form.Fields["Languages"] as PdfLoadedListBoxField).SelectedIndex = new int[1] { 2 }; (form.Fields["Associate degree"] as PdfLoadedCheckBoxField).Items[0].Checked = true; //Sign a PDF file PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[9] as PdfLoadedSignatureField; PdfCertificate certificate = new PdfCertificate(@"PDF.pfx", "syncfusion"); signatureField.Signature = new PdfSignature(loadedDocument, page, certificate, "Signature", signatureField); PdfBitmap signatureImage = new PdfBitmap(@"signature.jpg"); signatureField.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage, new PointF(5, 5), new SizeF(100, 20)); //Save the document loadedDocument.Save("Form.pdf"); //Close the document loadedDocument.Close(true); //This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("Form.pdf");
VB.NET
'Load the PDF document Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("FormTemplate.pdf") 'Gets the first page of the document Dim page As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage) 'Get the loaded form Dim form As PdfLoadedForm = loadedDocument.Form 'Fill the fields from the first page TryCast(form.Fields("Name"), PdfLoadedTextBoxField).Text = "Simons" TryCast(form.Fields("Email address"), PdfLoadedTextBoxField).Text = "simonsbistro@outlook.com" TryCast(form.Fields("Phone"), PdfLoadedTextBoxField).Text = "31 12 34 56" TryCast(form.Fields("Gender"), PdfLoadedRadioButtonListField).SelectedIndex = 0 TryCast(form.Fields("JobTitle"), PdfLoadedComboBoxField).SelectedIndex = 1 TryCast(form.Fields("Languages"), PdfLoadedListBoxField).SelectedIndex = New Integer(0) {2} TryCast(form.Fields("Associate degree"), PdfLoadedCheckBoxField).Items(0).Checked = True 'Sign a PDF file Dim signatureField As PdfLoadedSignatureField = TryCast(loadedDocument.Form.Fields(9), PdfLoadedSignatureField) Dim certificate As PdfCertificate = New PdfCertificate("PDF.pfx", "syncfusion") signatureField.Signature = New PdfSignature(loadedDocument, page, certificate, "Signature", signatureField) Dim signatureImage As PdfBitmap = New PdfBitmap("signature.jpg") signatureField.Signature.Appearance.Normal.Graphics.DrawImage(signatureImage, New PointF(5, 5), New SizeF(100, 20)) 'Save the document loadedDocument.Save("Form.pdf") 'Close the document loadedDocument.Close(True) 'This will open the PDF file so, the result will be seen in default PDF viewer Process.Start("Form.pdf")
You can download the work sample from PDFFormFillingSample.Zip.
By executing the program, you will get the PDF document as follows.
Take a moment to peruse the documentation for PDF Form filling and sign a PDF file, where you will find other options like modifying existing form fields, filling form fields, removing editing capability, removing form fields, importing FDF file to PDF, export PDF file to FDF, adding actions to form fields, and adding a timestamp in digital signature.
Refer here to explore the rich set of Syncfusion Essential PDF features.
An online sample link to filling form fields to personalize PDF document.
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.