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.

ASP.NET

Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.

ASP.NET Core
Angular
Blazor
JavaScript
React
  • Angular
  • Blazor
  • React
  • JavaScript
  • ASP.NET MVC, ASP.NET Core, and WebForms

Learn more Trial token Download trial

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 CorePDF

Validate Digital Signatures and the Integrity of PDF Documents in C# .NET

Learn how to validate digital signatures and the integrity of PDF documents using the PDF Validation component from TX Text Control in C# .NET. Ensure the authenticity and compliance of your…


ASP.NETASP.NET CorePDF

Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET

The new TXTextControl.PDF.Validation NuGet package enables you to validate PDF/UA documents and verify digital signatures directly in your code without relying on third-party tools or external…


ASP.NETASP.NET CoreC#

How To Choose the Right C# PDF Generation Library: Developer Checklist

To make your choice easy, this guide provides a systematic evaluation framework for two library categories: basic and enterprise PDF libraries. It covers matching features to use cases, evaluating…


ASP.NETASP.NET CoreDigital Signatures

Why Digitally Signing your PDFs is the Only Reliable Way to Prevent Tampering

PDF documents are widely used for sharing information because of their fixed layout and cross-platform compatibility. However, it is crucial to ensure the integrity and authenticity of these…

Summarize this blog post with:

Share on this blog post on: