# Generating Dynamic NDAs Using TX Text Control MailMerge in C# .NET

> This article demonstrates how to generate dynamic NDAs using TX Text Control MailMerge in C# .NET. It covers the process of creating a template, binding data, and generating the final document.

- **Author:** Bjoern Meyer
- **Published:** 2025-04-08
- **Modified:** 2025-11-16
- **Description:** This article demonstrates how to generate dynamic NDAs using TX Text Control MailMerge in C# .NET. It covers the process of creating a template, binding data, and generating the final document.
- **6 min read** (1086 words)
- **Tags:**
  - ASP.NET
  - ASP.NET Core
  - Contract
  - MailMerge
  - Merge Blocks
- **Web URL:** https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/
- **LLMs URL:** https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms.txt
- **LLMs-Full URL:** https://www.textcontrol.com/blog/2025/04/08/generating-dynamic-ndas-using-tx-text-control-mailmerge-in-csharp-dotnet/llms-full.txt
- **GitHub Repository:** https://github.com/TextControl/TXTextControl.Server.NDA.Dynamic

---

Dynamic contracts are a critical part of many data-driven applications. One of the most common examples is the non-disclosure agreement (NDA). While the structure of NDAs is fairly consistent, the content often needs to vary based on the business case, such as jurisdiction, clauses, or duration.

In this article, we'll show you how to build a dynamic NDA generator using TX Text Control's MailMerge engine, with a focus on using merge blocks and the RemoveEmptyBlocks property to include or exclude entire sections of a document based on input data.

### Dynamic, Data-Driven Contracts

The goal of this concept is to have a template with merge fields for dynamic data and also the ability to include specific sections in the agreement that are optional. With TX Text Control, you can create a single template that includes all possible sections and let the data itself control what is rendered. Hardcoding each variation would be inefficient and error-prone.

Here's a breakdown of a typical NDA and the sections that can be dynamically controlled using merge fields or conditional blocks:

| Section | Dynamic? | Merge Fields / Conditions |
|---|---|---|
| Introduction / Preamble | No | *«effective\_date»*   *«disclosing\_party.name»*   *«disclosing\_party.address»*   *«receiving\_party.name»*   *«receiving\_party.address»* |
| Purpose | No | *«purpose»* |
| Definition of Confidential Information | No |  |
| Obligations of Receiving Party | No |  |
| Term and Termination | Yes | *«term»*   *«survival»* |
| Return or Destruction of Materials | Yes |  |
| Non-Compete and Non-Solicitation | Yes | *«duration»* |
| Governing Law | Yes | *«jurisdiction»* |
| Entire Agreement | No |  |
| Signatures | Yes | *«disclosing\_party.company»*   *«disclosing\_party.name»*   *«disclosing\_party.title»*   *«disclosing\_party.date»*   *«receiving\_party.company»*   *«receiving\_party.name»*   *«receiving\_party.title»*   *«receiving\_party.date»* |

### Creating the Template

The first part of the template looks very straightforward, with structured numbered lists and merge fields as placeholders where the actual data will be merged.

![NDA template](https://s1-www.textcontrol.com/assets/dist/blog/2025/04/08/a/assets/template1.webp "NDA template")

Now let us look at the dynamic part of the contract template. The dynamic sections consist of merge blocks (highlighted in red by default) with optional merge fields.

![NDA template with dynamic sections](https://s1-www.textcontrol.com/assets/dist/blog/2025/04/08/a/assets/template2.webp "NDA template with dynamic sections")

### Using Merge Blocks

Pay close attention to what is in the merge block. It includes the last carriage return line feed and an extra carriage return character, which is necessary because we may be removing content from a numbered list. So in both cases, whether the content remains or is removed, the list is perfectly intact.

![NDA template with dynamic sections](https://s1-www.textcontrol.com/assets/dist/blog/2025/04/08/a/assets/template3.webp "NDA template with dynamic sections")

### Using the MailMerge Engine

For demonstration purposes, we will use the following JSON data as our sample data for the merge process. This JSON data contains all the data needed to include all the sections.

```
[
    {
        "effective_date": "2025-10-01",
        "disclosing_party": {
            "name": "Company A",
            "address": "1234 Elm St, Springfield, IL 62701"
        },
        "receiving_party": {
            "name": "Company B",
            "address": "5678 Oak St, Springfield, IL 62702"
        },
        "purpose": "the evaluating of a potential business relationship",

        "section_return": {},
        "section_compete": {
            "duration": 12
        },
        "section_law": {
            "jurisdiction": "Illinois"
        },
        "section_signatures": {
            "disclosing_party": {
                "company": "Company A",
                "name": "John Doe",
                "title": "CEO",
                "date": "2025-10-01"
            },
            "receiving_party": {
                "company": "Company B",
                "name": "Jane Smith",
                "title": "CFO",
                "date": "2025-10-01"
            }
        }
    }
]
```

The following code is required to merge the JSON data into the template using the MailMerge class.

```
using TXTextControl.DocumentServer;
using TXTextControl;

using ServerTextControl tx = new();

// Create a new document instance in memory
tx.Create();

// Load NDA template (TX Text Control format)
tx.Load("Data/nda_template.tx", StreamType.InternalUnicodeFormat);

// Load JSON data for MailMerge from file
string jsonData = File.ReadAllText("Data/nda_data.json");

// Initialize MailMerge with the document
MailMerge mailMerge = new()
{
    TextComponent = tx,
    RemoveEmptyBlocks = true, // Removes blocks that remain empty after merge
};

// Track merged block names
List<string> blockNames = new();

mailMerge.BlockMerging += (sender, e) =>
{
    blockNames.Add(e.BlockName);
};

// Merge JSON data into the loaded template
mailMerge.MergeJsonData(jsonData);

// Export the filled NDA to a PDF file
tx.Save("results.pdf", StreamType.AdobePDF);

// Output result to console
Console.WriteLine(blockNames.Count > 0
    ? $"Merged {blockNames.Count} blocks: {string.Join(", ", blockNames)}"
    : "No blocks were merged.");
```

If you use the full JSON with all sections, the output should be this and a PDF is generated.

```
Merged 5 blocks: section_signatures, section_law, section_compete, section_return, section_term
```

In the created PDF you can see that all merge fields are filled and all sections are included.

![NDA template with all sections](https://s1-www.textcontrol.com/assets/dist/blog/2025/04/08/a/assets/output1.webp "NDA template with all sections")

### Removing Sections

Now let's remove the *section\_term* and *section\_compete* sections from the JSON and merge it back together. The JSON now looks like this:

```
[
    {
        "effective_date": "2025-10-01",
        "disclosing_party": {
            "name": "Company A",
            "address": "1234 Elm St, Springfield, IL 62701"
        },
        "receiving_party": {
            "name": "Company B",
            "address": "5678 Oak St, Springfield, IL 62702"
        },
        "purpose": "the evaluating of a potential business relationship",
        "section_return": {},
        "section_law": {
            "jurisdiction": "Illinois"
        },
        "section_signatures": {
            "disclosing_party": {
                "company": "Company A",
                "name": "John Doe",
                "title": "CEO",
                "date": "2025-10-01"
            },
            "receiving_party": {
                "company": "Company B",
                "name": "Jane Smith",
                "title": "CFO",
                "date": "2025-10-01"
            }
        }
    }
]
```

And the output is this:

```
Merged 3 blocks: section_signatures, section_law, section_return
```

As you can see, the sections are removed from the PDF and the list is still intact.

![NDA template with some sections removed](https://s1-www.textcontrol.com/assets/dist/blog/2025/04/08/a/assets/output2.webp "NDA template with some sections removed")

The result is a perfectly designed contract that is completely dynamic and 100% controlled by the data.

### Benefits of This Approach

This approach allows you to create a single template that can handle multiple variations of a contract. This reduces the need for multiple templates and simplifies the contract creation process. It also allows for easy updates to the template without having to modify multiple files.

- **Reusability:** One template covers hundreds of NDA variations.
- **Accuracy:** Reduces manual errors by automating document assembly.
- **Efficiency:** Generate a polished, branded NDA in seconds.
- **Flexibility:** Easily update legal language across all generated NDAs.

### Conclusion

In summary, using TX Text Control's MailMerge engine with dynamic merge blocks allows you to create flexible and efficient contracts that can adapt to different business needs. This not only saves time, but also ensures that your contracts are always up to date and compliant with the latest legal standards.

The sample code and templates provided in this article can be used as a starting point for your own NDA generator. Feel free to modify the template and JSON data to suit your specific requirements.

---

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

- [Designing the Perfect Contract Template for MailMerge in C#](https://www.textcontrol.com/blog/2024/09/26/designing-the-perfect-contract-template-for-mailmerge-in-csharp/llms.txt)
- [Filtering and Sorting Repeating Blocks in MailMerge using C#](https://www.textcontrol.com/blog/2025/08/21/filtering-and-sorting-repeating-blocks-in-mailmerge-using-csharp/llms.txt)
- [Use MailMerge in .NET on Linux to Generate Pixel-Perfect PDFs from DOCX Templates](https://www.textcontrol.com/blog/2025/05/27/use-mailmerge-in-dotnet-on-linux-to-generate-pixel-perfect-pdfs-from-docx-templates/llms.txt)
- [Designing a Maintainable PDF Generation Web API in ASP.NET Core (Linux) C# with Clean Architecture and TX Text Control](https://www.textcontrol.com/blog/2025/03/27/designing-a-maintainable-pdf-generation-web-api-in-asp-net-core-linux-c-sharp-with-clean-architecture-and-tx-text-control/llms.txt)
- [Manipulating Table Cells During the MailMerge Process in .NET C#](https://www.textcontrol.com/blog/2024/12/03/manipulating-table-cells-during-the-mailmerge-process-in-net-csharp/llms.txt)
- [When to Generate Documents Server-Side Instead of Client-Side: A Focus on Data Security](https://www.textcontrol.com/blog/2024/10/04/when-to-generate-documents-server-side-instead-of-client-side-a-focus-on-data-security/llms.txt)
- [Mail Merge: Inserting Merge Blocks using the DataSourceManager in C#](https://www.textcontrol.com/blog/2024/10/02/mail-merge-inserting-merge-blocks-using-the-datasourcemanager-in-csharp/llms.txt)
- [Creating Advanced Tables in PDF and DOCX Documents with C#](https://www.textcontrol.com/blog/2024/09/30/creating-advanced-tables-in-pdf-and-docx-documents-with-csharp/llms.txt)
- [Video Tutorial: Creating a MailMerge Template and JSON Data Structure](https://www.textcontrol.com/blog/2024/08/16/video-tutorial-creating-a-mailmerge-template-and-json-data-structure/llms.txt)
- [Getting Started Video Tutorial: How to use the MailMerge and ServerTextControl Classes in ASP.NET Core C#](https://www.textcontrol.com/blog/2024/08/05/getting-started-video-tutorial-how-to-use-the-mailmerge-and-servertextcontrol-classes-in-asp-net-core-c/llms.txt)
- [Getting Started Videos: New Text Control YouTube Channel](https://www.textcontrol.com/blog/2024/08/02/getting-started-videos-new-text-control-youtube-channel/llms.txt)
- [Best Practices for Mail Merge and Form Field Processing in ASP.NET Core C# Applications](https://www.textcontrol.com/blog/2024/07/30/best-practices-for-mail-merge-and-form-field-processing-in-asp-net-core-csharp-applications/llms.txt)
- [Advantages of Flow Type Layout Reporting vs. Banded Reporting or PDF Template Engines in .NET C#](https://www.textcontrol.com/blog/2024/07/29/advantages-of-flow-type-layout-reporting-vs-banded-reporting-or-pdf-template-engines-in-net-c-sharp/llms.txt)
- [Designing a MailMerge Web API Endpoint with ASP.NET Core in C#](https://www.textcontrol.com/blog/2024/07/12/designing-a-mailmerge-web-api-endpoint-with-asp-net-core-in-c-sharp/llms.txt)
- [Enhancing Documents with QR Codes and Barcodes in .NET C#: A Comprehensive Guide](https://www.textcontrol.com/blog/2024/07/11/enhancing-documents-with-qr-codes-and-barcodes-in-net-csharp-a-comprehensive-guide/llms.txt)
- [Document Automation 101: Leveraging TX Text Control for Business Efficiency in .NET C# Applications](https://www.textcontrol.com/blog/2024/07/09/document-automation-101-leveraging-tx-text-control-for-business-efficiency-in-net-c-applications/llms.txt)
- [Merging Templates with MailMerge with Different Merge Field Settings in C#](https://www.textcontrol.com/blog/2023/12/16/merging-templates-with-mailmerge-with-different-merge-field-settings/llms.txt)
- [How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#](https://www.textcontrol.com/blog/2023/10/16/how-to-mail-merge-ms-word-docx-documents-in-aspnet-core-csharp/llms.txt)
- [MailMerge: Working with Image Placeholders](https://www.textcontrol.com/blog/2022/12/22/mailmerge-working-with-image-placeholders/llms.txt)
- [Getting Started: ServerTextControl and MailMerge with ASP.NET Core](https://www.textcontrol.com/blog/2022/09/01/getting-started-servertextcontrol-and-mailmerge-with-aspnet-core/llms.txt)
- [MailMerge: Data Structures Explained with a Sample Template and JSON Data](https://www.textcontrol.com/blog/2022/03/14/mailmerge-data-structures-explained-with-a-sample-template-and-json-data/llms.txt)
- [Adding SVG Watermarks to Documents](https://www.textcontrol.com/blog/2022/01/28/adding-svg-watermarks-to-documents/llms.txt)
- [Using MailMerge in ASP.NET Core 6 Web Applications](https://www.textcontrol.com/blog/2022/01/27/using-mailmerge-in-aspnet-core-6-web-applications/llms.txt)
