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

- **Author:** Bjoern Meyer
- **Published:** 2023-07-05
- **Modified:** 2026-04-06
- **Description:** 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.
- **3 min read** (563 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - Electronic Signatures
  - PDF
- **Web URL:** https://www.textcontrol.com/blog/2023/07/05/preparing-documents-for-electronic-signatures-using-mailmerge-in-csharp/
- **LLMs URL:** https://www.textcontrol.com/blog/2023/07/05/preparing-documents-for-electronic-signatures-using-mailmerge-in-csharp/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2023/07/05/preparing-documents-for-electronic-signatures-using-mailmerge-in-csharp/llms-full.txt

---

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](https://s1-www.textcontrol.com/assets/dist/blog/2023/07/05/a/assets/template.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/07/05/a/assets/viewer2.webp "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](https://s1-www.textcontrol.com/assets/dist/blog/2023/07/05/a/assets/results.webp "Creating documents with TX Text Control")

---

## About Bjoern Meyer

As CEO, Bjoern is the visionary behind our strategic direction and business development, bridging the gap between our customers and engineering teams. His deep passion for coding and web technologies drives the creation of innovative products. If you're at a tech conference, be sure to stop by our booth - you'll most likely meet Bjoern in person. With an advanced graduate degree (Dipl. Inf.) in Computer Science, specializing in AI, from the University of Bremen, Bjoern brings significant expertise to his role. In his spare time, Bjoern enjoys running, paragliding, mountain biking, and playing the piano.

- [LinkedIn](https://www.linkedin.com/in/bjoernmeyer/)
- [X](https://x.com/txbjoern)
- [GitHub](https://github.com/bjoerntx)

---

## Related Posts

- [Replace Words at the Input Position with Formatted Content from a Web API](https://www.textcontrol.com/blog/2023/07/06/replace-words-at-the-input-position-with-formatted-content-from-a-web-api/llms.txt)
- [Why Structured E-Invoices Still Need Tamper Protection using C# and .NET](https://www.textcontrol.com/blog/2026/03/24/why-structured-e-invoices-still-need-tamper-protection-using-csharp-and-dotnet/llms.txt)
- [Create Fillable PDFs from HTML Forms in C# ASP.NET Core Using a WYSIWYG Template](https://www.textcontrol.com/blog/2026/03/17/create-fillable-pdfs-from-html-forms-in-csharp-aspnet-core-using-a-wysiwyg-template/llms.txt)
- [Why HTML to PDF Conversion is Often the Wrong Choice for Business Documents in C# .NET](https://www.textcontrol.com/blog/2026/03/13/why-html-to-pdf-conversion-is-often-the-wrong-choice-for-business-documents-in-csharp-dot-net/llms.txt)
- [A Complete Guide to Converting Markdown to PDF in .NET C#](https://www.textcontrol.com/blog/2026/01/07/a-complete-guide-to-converting-markdown-to-pdf-in-dotnet-csharp/llms.txt)
- [Why PDF Creation Belongs at the End of the Business Process](https://www.textcontrol.com/blog/2026/01/02/why-pdf-creation-belongs-at-the-end-of-the-business-process/llms.txt)
- [Designing the Perfect PDF Form with TX Text Control in .NET C#](https://www.textcontrol.com/blog/2025/12/16/designing-the-perfect-pdf-form-with-tx-text-control-in-dotnet-csharp/llms.txt)
- [Why Defining MIME Types for PDF/A Attachments Is Essential](https://www.textcontrol.com/blog/2025/12/10/why-defining-mime-types-for-pdfa-attachments-is-essential/llms.txt)
- [Validate Digital Signatures and the Integrity of PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/11/14/validate-digital-signatures-and-the-integrity-of-pdf-documents-in-csharp-dotnet/llms.txt)
- [Validate PDF/UA Documents and Verify Electronic Signatures in C# .NET](https://www.textcontrol.com/blog/2025/11/13/validate-pdf-ua-documents-and-verify-electronic-signatures-in-csharp-dotnet/llms.txt)
- [How To Choose the Right C# PDF Generation Library: Developer Checklist](https://www.textcontrol.com/blog/2025/11/12/how-to-choose-the-right-csharp-pdf-generation-library-developer-checklist/llms.txt)
- [Why Digitally Signing your PDFs is the Only Reliable Way to Prevent Tampering](https://www.textcontrol.com/blog/2025/10/30/why-digitally-signing-your-pdfs-is-the-only-reliable-way-to-prevent-tampering/llms.txt)
- [Automating PDF/UA Accessibility with AI: Describing DOCX Documents Using TX Text Control and LLMs](https://www.textcontrol.com/blog/2025/10/16/automating-pdf-ua-accessibility-with-ai-describing-docx-documents-using-tx-text-control-and-llms/llms.txt)
- [Converting Office Open XML (DOCX) to PDF in Java](https://www.textcontrol.com/blog/2025/10/14/converting-office-open-xml-docx-to-pdf-in-java/llms.txt)
- [Extending DS Server with Custom Digital Signature APIs](https://www.textcontrol.com/blog/2025/10/09/extending-ds-server-with-custom-digital-signature-apis/llms.txt)
- [Why PDF/UA and PDF/A-3a Matter: Accessibility, Archiving, and Legal Compliance](https://www.textcontrol.com/blog/2025/10/07/why-pdf-ua-and-pdf-a-3a-matter-accessibility-archiving-and-legal-compliance/llms.txt)
- [Convert Markdown to PDF in a Console Application on Linux and Windows](https://www.textcontrol.com/blog/2025/09/23/convert-markdown-to-pdf-in-a-console-application-on-linux-and-windows/llms.txt)
- [Mining PDFs with Regex in C#: Practical Patterns, Tips, and Ideas](https://www.textcontrol.com/blog/2025/08/12/mining-pdfs-with-regex-in-csharp-practical-patterns-tips-and-ideas/llms.txt)
- [Streamline Data Collection with Embedded Forms in C# .NET](https://www.textcontrol.com/blog/2025/08/02/streamline-data-collection-with-embedded-forms-in-csharp-dotnet/llms.txt)
- [Adding QR Codes to PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/07/15/adding-qr-codes-to-pdf-documents-in-csharp-dotnet/llms.txt)
- [Adding SVG Graphics to PDF Documents in C# .NET](https://www.textcontrol.com/blog/2025/07/08/adding-svg-graphics-to-pdf-documents-in-csharp-dotnet/llms.txt)
- [Enhancing PDF Searchability in Large Repositories by Adding and Reading Keywords Using C# .NET](https://www.textcontrol.com/blog/2025/06/24/enhancing-pdf-searchability-in-large-repositories-by-adding-and-reading-keywords-using-csharp-dotnet/llms.txt)
- [How to Verify PDF Encryption Programmatically in C# .NET](https://www.textcontrol.com/blog/2025/06/20/how-to-verify-pdf-encryption-programmatically-in-csharp-dotnet/llms.txt)
- [PDF Security for C# Developers: Encryption and Permissions in .NET](https://www.textcontrol.com/blog/2025/06/16/pdf-security-for-csharp-developers-encryption-and-permissions-in-dotnet/llms.txt)
- [Add JavaScript to PDFs with TX Text Control in C# .NET: Time-Based Alerts Made Easy](https://www.textcontrol.com/blog/2025/06/13/add-javascript-to-pdfs-with-tx-text-control-in-c-dot-net-time-based-alerts-made-easy/llms.txt)
