How to add signature field in the PDF converted from Word
Syncfusion Essential DocIO is a .NET Word library used to create, read, and edit Word documents programmatically without Microsoft Word or interop dependencies.
You can add a signature field in the PDF document which is converted from Word in C# and VB.NET. The below steps should be followed.
- Design your template Word document with the required layout of form fields using Microsoft Word.
- During Word to PDF conversion, enable the flag PreserveFormFields to preserve form fields in the converted PDF using Essential DocIO.
- Using the Syncfusion PDF library, you can replace the form fields with PdfSignatureField by finding the corresponding form fields in the PDF document.
Steps to add a signature field in the PDF converted from Word using C# and VB.NET:
- Create a new C# console application project.
- Install the Syncfusion.DocToPDFConverter.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.
- Include the following namespace in the Program.cs file.
C#
using Syncfusion.DocIO; using Syncfusion.DocIO.DLS; using Syncfusion.DocToPDFConverter; using Syncfusion.Pdf; using Syncfusion.Pdf.Interactive; using Syncfusion.Pdf.Parsing;
VB
Imports Syncfusion.DocIO Imports Syncfusion.DocIO.DLS Imports Syncfusion.DocToPDFConverter Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Interactive Imports Syncfusion.Pdf.Parsing
- Use the following code to add a signature field in the PDF document converted from a Word document.
C#
string inpuFile = @"TemplateSign.docx"; //Open the template document. using (WordDocument document = new WordDocument(inpuFile, FormatType.Docx)) { //Create an instance of the DocToPDFConverter. DocToPDFConverter converter = new DocToPDFConverter(); //Enable the flag to preserve the text form field. converter.Settings.PreserveFormFields = true; //Convert Word document to PDF. PdfDocument pdf = converter.ConvertToPDF(document); //Dispose the resources. converter.Dispose(); //Save the PDF document. MemoryStream stream = new MemoryStream(); pdf.Save(stream); stream.Position = 0; //Add a signature field to the PDF. AddSignature(stream); pdf.Close(); } System.Diagnostics.Process.Start(@"OutputSign.pdf");
VB
Dim inpuFile As String = "TemplateSign.docx" 'Open the template document. Using document As WordDocument = New WordDocument(inpuFile, FormatType.Docx) 'Create an instance of the DocToPDFConverter. Dim converter As DocToPDFConverter = New DocToPDFConverter 'Enable the flag to preserve the text form field. converter.Settings.PreserveFormFields = True 'Convert Word document to PDF. Dim pdf As PdfDocument = converter.ConvertToPDF(document) 'Dispose the resources. converter.Dispose 'Save the PDF document. Dim stream As MemoryStream = New MemoryStream pdf.Save(stream) stream.Position = 0 'Add a signature field to the PDF. AddSignature(stream) pdf.Close() End Using System.Diagnostics.Process.Start("OutputSign.pdf")
- Include the following helper method to insert signature field using Syncfusion PDF library.
C#
private static void AddSignature(MemoryStream stream) { //Load the PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream); //Get the loaded form. PdfLoadedForm loadedForm = loadedDocument.Form; for (int i = 0; i < loadedForm.Fields.Count; i++) { if (loadedForm.Fields[i] is PdfLoadedTextBoxField) { //Get the loaded text box field and fill it. PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[i] as PdfLoadedTextBoxField; //Get bounds from an existing textbox field. RectangleF bounds = loadedTextBoxField.Bounds; //Get the page. PdfPageBase loadedPage = loadedTextBoxField.Page; //Create a PDF Signature field. PdfSignatureField signatureField = new PdfSignatureField(loadedPage, loadedTextBoxField.Text.Trim()); //Set properties to the signature field. signatureField.Bounds = bounds; //Add the form field to the document. loadedDocument.Form.Fields.Add(signatureField); } } //Save the document. loadedDocument.Save(@"OutputSign.pdf"); //Close the document. loadedDocument.Close(true); }
VB
Private Shared Sub AddSignature(ByVal stream As MemoryStream) 'Load the PDF document. Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument(stream) 'Get the loaded form. Dim loadedForm As PdfLoadedForm = loadedDocument.Form Dim i As Integer = 0 Do While (i < loadedForm.Fields.Count) If (TypeOf loadedForm.Fields(i) Is PdfLoadedTextBoxField) Then 'Get the loaded text box field and fill it. Dim loadedTextBoxField As PdfLoadedTextBoxField = CType(loadedForm.Fields(i), PdfLoadedTextBoxField) 'Get bounds from an existing textbox field. Dim bounds As RectangleF = loadedTextBoxField.Bounds 'Get the page. Dim loadedPage As PdfPageBase = loadedTextBoxField.Page 'Create a PDF Signature field. Dim signatureField As PdfSignatureField = New PdfSignatureField(loadedPage, loadedTextBoxField.Text.Trim) 'Set properties to the signature field. signatureField.Bounds = bounds 'Add the form field to the document. loadedDocument.Form.Fields.Add(signatureField) End If i = (i + 1) Loop 'Save the document. loadedDocument.Save("OutputSign.pdf") 'Close the document. loadedDocument.Close(True) End Sub
A complete working example of how to add a signature field in the PDF document using C# can be downloaded from add signature field in the PDF document.zip
By executing the program, you will get the output PDF document with a Signature field as follows.
Take a moment to peruse the documentation, where you can find basic Word document processing options along with the features like mail merge, merge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, PDF and Image conversions with code examples.
Explore more about the rich set of Syncfusion Word Framework features.
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 how to generate and register a Syncfusion license key in your application to use the components without a trail message.