Products Technologies Demo Docs Blog Support Company

Preparing Documents for Electronic Signatures using MailMerge in C#

There are many benefits to using MS Word compatible templates to prepare documents for electronic signature capture. This article shows how to use MailMerge to prepare documents for the signing process.

Preparing Documents for Electronic Signatures using MailMerge in C#

Companies and organizations in every industry are recognizing the value of integrating electronic signatures into their workflows. Many e-signature services start the signature process with a pre-generated PDF document that comes from applications such as MS Word or ERP systems.

Dynamic Templates

However, a document should be able to be edited for as long as possible. It is a dynamic structure that should be able to be updated at any time until the process is complete and a document is created for further processing or archiving.

MS Word Compatible

Instead of a sealed PDF document, the document signing process starts with an MS Word compatible template. This gives developers the flexibility they need to integrate the entire pipeline of digital document processing into their own workflows. Typically, the signature process starts with a template consisting of merge field placeholders and form fields that are pre-populated before the generated document is passed to the signature workflow.

A simple template consisting of a standard layout, static data, dynamic merge, and form fields is shown in the following screenshot:

Creating documents with TX Text Control

Pre-Populate Fields

Before you use the Document Viewer to deliver this document to the end user, you can prepare it by merging the data you know into the document. The following code uses the MailMerge class to merge a data object into the template.

byte[] document;
        
using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
  tx.Create();
  tx.Load("App_Data/vaccination_form.tx", TXTextControl.StreamType.InternalUnicodeFormat);

  using (TXTextControl.DocumentServer.MailMerge mm =
    new TXTextControl.DocumentServer.MailMerge()) {
    mm.TextComponent = tx;
    mm.FormFieldMergeType = TXTextControl.DocumentServer.FormFieldMergeType.Preselect;
    mm.RemoveEmptyFields = false;

    // pre-select form fields with known data and merge
    // data into merge fields
    mm.MergeObject(data);
  }

  tx.Save(out document, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
}

Capturing Signatures

The pre-populated document is then loaded into the Document Viewer to capture user data and the final signature.

Creating documents with TX Text Control

Digital Signing

When the document is submitted, the Document Viewer automatically merges the completed form field values and captured signature into the document. Finally, server-side certificate signing is performed to retrieve a fully digitally signed and tamper-proofed PDF document.

[HttpPost]
public string ExportPDF()
{
  Stream inputStream = Request.InputStream;
  inputStream.Position = 0;

  StreamReader str = new StreamReader(inputStream);
  string sBuf = str.ReadToEndAsync().Result;

  TXTextControl.Web.MVC.DocumentViewer.Models.SignatureData data =
    JsonConvert.DeserializeObject<TXTextControl.Web.MVC.DocumentViewer.Models.SignatureData>(sBuf);

  byte[] bPDF;

  // create temporary ServerTextControl
  using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
  {
    tx.Create();

    // load the document
    tx.Load(Convert.FromBase64String(data.SignedDocument.Document), TXTextControl.BinaryStreamType.InternalUnicodeFormat);

    FlattenFormFields(tx);

    X509Certificate2 cert = new X509Certificate2(Server.MapPath("~/App_Data/textcontrolself.pfx"), "123");

    TXTextControl.SaveSettings saveSettings = new TXTextControl.SaveSettings()
    {
      CreatorApplication = "TX Text Control Sample Application",
      //DigitalSignature = new TXTextControl.DigitalSignature(cert, null),
           SignatureFields = new DigitalSignature[] { 
              new TXTextControl.DigitalSignature(cert, null, "txsign"),
              new TXTextControl.DigitalSignature(cert, null, "txsign_init")}
    };

    // save the document as PDF
    tx.Save(out bPDF, TXTextControl.BinaryStreamType.AdobePDFA, saveSettings);
  }

  // return as Base64 encoded string
  return Convert.ToBase64String(bPDF);
}

When you open the generated document, you can see the validity of the signature fields in the Acrobat Reader.

Creating documents with TX Text Control

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETASP.NET CoreElectronic Signatures

Replace Words at the Input Position with Formatted Content from a Web API

Writing assistance and placeholder replacement are advanced features that help users create professional documents. This sample shows how to replace the word at the current input position with…


ASP.NETASP.NET CoreExtraction

Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas

Mining PDFs with Regex in C# can be a powerful technique for extracting information from documents. This article explores practical patterns, tips, and ideas for effectively using regular…


ASP.NETASP.NET CoreForms

Streamline Data Collection with Embedded Forms in C# .NET

Discover how to enhance your C# .NET applications by embedding forms for data collection. This article explores the benefits of using Text Control's ASP.NET and ASP.NET Core components to create…


ASP.NETASP.NET CorePDF

Adding QR Codes to PDF Documents in C# .NET

This article explains how to add QR codes to PDF documents with the Text Control .NET Server component in C#. It provides the necessary steps and code snippets for effectively implementing this…


ASP.NETASP.NET CorePDF

Adding SVG Graphics to PDF Documents in C# .NET

In this article, we will explore how to add SVG graphics to PDF documents using C# .NET. We will use the TX Text Control .NET Server component to demonstrate the process of rendering SVG images in…