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.

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:

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 Mail
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.

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.

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.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
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 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…
Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX…
This article shows how to use TX Text Control together with the OpenAI API to automatically add descriptive texts (alt text and labels) to images, links, and tables in a DOCX. The resulting…
Converting Office Open XML (DOCX) to PDF in Java
Learn how to convert Office Open XML (DOCX) documents to PDF in Java using the powerful ServerTextControl library. This guide provides step-by-step instructions and code examples to help you…
Extending DS Server with Custom Digital Signature APIs
In this article, we will explore how to extend the functionality of DS Server by integrating custom digital signature APIs. We will cover the necessary steps to create a plugin that allows DS…
