We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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:

  1. Create a new C# console application project. Create Console application in Visual Studio 
  2. Install the Syncfusion.DocToPDFConverter.WinForms NuGet package as a reference to your .NET Framework applications from NuGet.org.

Add DocIO NuGet package reference to the project

  1. 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
  1. 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")

 

  1. 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.
Graphical user interface, text, application, email

Description automatically generated

 

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.

Note:

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.

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile