Articles in this section
Category / Section

How to execute mail merge, convert to PDF, and add digital signatures for each entry?

6 mins read

Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can execute mail merge, convert to PDF, and add digital signatures for each entry using C#.

To achieve this, insert a text form field at the desired signature location in the input Word document. After performing mail merge and converting the document to PDF, identify and replace each text form field with a digital signature field to enable signing for each mail merge entry.

Steps to execute mail merge, convert to PDF, and add digital signatures for each entry:

  1. Create a new .NET Core console application project.
    Create console application in Visual Studio
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
    Add DocIO NuGet package reference to the project
Note:

Starting with v16.2.0.x, if you reference Syncfusion® assemblies from a trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without a trial message.

  1. Include the following namespaces in the Program.cs file.
    C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing; 
  1. Use the following code example to execute mail merge, convert to PDF, and add digital signatures for each entry.
    C#
// Load the Word document
using (WordDocument document = new WordDocument(Path.GetFullPath(@"Data/Template.docx")))
{
    // Get the recipient details as a list of .NET objects
    List<Recipient> recipients = GetRecipients();
    // Perform mail merge using the recipient data
    document.MailMerge.Execute(recipients);
    // Initialize the DocIO to PDF converter
    using (DocIORenderer renderer = new DocIORenderer())
    {
        // Preserve form fields (textboxes, checkboxes, etc.)
        renderer.Settings.PreserveFormFields = true;
        // Convert the merged Word document to PDF
        using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
        {
            // Save the PDF to memory stream
            using (MemoryStream stream = new MemoryStream())
            {
                pdfDocument.Save(stream);
                stream.Position = 0;
                // Add digital signature fields in place of textboxes
                AddSignatureField(stream, "Signature");
            }
        }
    }
}
  1. Use the following code example to get the data to perform mail merge.
    C#
List<Recipient> GetRecipients()
{
    List<Recipient> recipients = new List<Recipient>();
    // Initialize sample recipient data
    recipients.Add(new Recipient("Nancy Davolio", "NorthWinds", "507 - 20th Ave. E.Apt. 2A", "507-345-2309"));
    recipients.Add(new Recipient("Janet Leverling", "NorthWinds", "722 Moss Bay Blvd.", "542-754-2843"));
    return recipients;
}
  1. Use the following code example to replace text box fields in the PDF with signature fields at the same positions.
    C#
void AddSignatureField(MemoryStream stream,  string bookmarkTitle)
{
    // Load the generated PDF document
    using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream))
    {
        // Access the form fields in the PDF
        PdfLoadedForm loadedForm = loadedDocument.Form;

        // Loop through each form field
        for (int i = loadedForm.Fields.Count - 1; i >= 0; i--)
        {
            if (loadedForm.Fields[i] is PdfLoadedTextBoxField)
            {
                // Get the loaded text box field
                PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[i] as PdfLoadedTextBoxField;
                // Get its bounds and page to use for placing the signature field
                RectangleF bounds = loadedTextBoxField.Bounds;
                PdfPageBase loadedPage = loadedTextBoxField.Page;
                // Create a signature field with the same name and location
                PdfSignatureField signatureField = new PdfSignatureField(loadedPage, loadedTextBoxField.Text.Trim());
                signatureField.Bounds = bounds;
                // Add the signature field to the document
                loadedDocument.Form.Fields.Add(signatureField);
                // Remove the original textbox field
                loadedDocument.Form.Fields.Remove(loadedTextBoxField);
            }
        }
        // Save the modified PDF with signature fields
        using (FileStream outputFile = new FileStream(Path.GetFullPath(@"../../../Output/Result.pdf"), FileMode.Create))
        {
            loadedDocument.Save(outputFile);
        }
    }
}
  1. Use the following class to represent the Recipient details for the mail merge.
    C#
class Recipient
{
    public string FirstName { get; set; }
    public string CompanyName { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }

    public Recipient(string firstName, string companyName, string address, string phone)
    {
        FirstName = firstName;
        CompanyName = companyName;
        Address = address;
        PhoneNumber = phone;
    }
}

You can download a complete working sample to execute mail merge, convert to PDF, and add digital signatures for each entry from GitHub.

Output Word document

Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

See Also

Conclusion

I hope you enjoyed learning about how to execute mail merge, convert to PDF, and add digital signature for each entry.

You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications. You can also explore our ASP.NET Core DocIO example 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 check out 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, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied