2X faster development
The ultimate WinForms UI toolkit to boost your development speed.
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.
Steps to add a signature field in the PDF converted from Word using C# and VB.NET:
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
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")
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. 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.
This page will automatically be redirected to the sign-in page in 10 seconds.