# ASP.NET MVC Mail Merge

> Use ServerTextControl and MailMerge in an ASP.NET MVC controller on the .NET Framework: assembly references, licenses.licx, JSON merge, HTML or PDF output.

- **Product:** TX Text Control .NET Server
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET Server > Integration Guide > ASP.NET on .NET Framework > ASP.NET MVC Mail Merge
- **2983 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/integration/dotnet-framework/mvc-mail-merge/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/integration/dotnet-framework/mvc-mail-merge/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/integration/dotnet-framework/mvc-mail-merge/llms-full.txt

---

`ServerTextControl` and `MailMerge` run in the controllers of an ASP.NET MVC 5 application on the .NET Framework as they do in ASP.NET Core: the controller creates a document, merges data into it and returns the result. What follows builds that application in Visual Studio 2022 with .NET Framework 4.8. The controller writes a bold heading and a merge field, fills the field from JSON data and shows the merged document as HTML in the view.

**Legacy technology.** The .NET Framework version of TX Text Control .NET Server runs on Windows only and uses the Classic engine. For a new application, start with [Create Your First Application](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/first-application/llms-full.txt) and [Simple Mail Merge](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/mail-merge/simple/llms-full.txt), which use the NuGet packages.

### Prerequisites

- Visual Studio 2022 and .NET Framework 4.8.
- The Windows setup of TX Text Control .NET Server 34.0, trial or full version. It installs the assemblies to `C:\Program Files\Text Control GmbH\TX Text Control 34.0.NET Server for ASP.NET\`.

The setup installs this tutorial as a sample project in `%USERPROFILE%\Documents\TX Text Control 34.0.NET Server for ASP.NET\CSharp\MVC\mailmerge\`.

The project uses no NuGet package of TX Text Control. It references the assemblies of the setup and needs neither the TX TextControl Service nor a WebSocket connection, because no editor runs in the browser.

### Create the Project

1. In Visual Studio 2022, choose **Create a new project**.
2. Select **ASP.NET Web Application (.NET Framework)** as the project template and confirm with **Next**.
3. Enter a project name, select **.NET Framework 4.8** as the **Framework** and confirm with **Create**.
4. In the next dialog, choose **MVC** as the project template and confirm with **Create**.

    ![The Create a new ASP.NET Web Application dialog of Visual Studio with the MVC template selected and only MVC checked under Add folders & core references.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-server/aspnet-template-mvc.18b14a9c.webp)

### Add the TX Text Control References

1. With the project selected in **Solution Explorer**, choose **Project > Add Project Reference...** and click **Browse...** in the **Reference Manager**.
2. From the *Assembly* folder of the installation, select *TXDocumentServer.dll*, *TXTextControl.dll* and *TXTextControl.Server.dll*.
3. From the *Assembly\\bin64* folder, select *tx34.dll*, *tx34\_ic.dll* and *tx34\_tls.dll*.
4. Close the **Reference Manager** with **OK**.

*TXTextControl.Server.dll* contains `ServerTextControl`, and *TXDocumentServer.dll* contains `MailMerge`. The three files from *bin64* are the 64-bit native files of the engine; a 32-bit application uses the files of the same names from *Assembly\\bin*.

### Add the License File

1. Choose **Project > Add New Item...**, select **Text File**, name it *licenses.licx* and click **Add**.

    ![The Add New Item dialog of Visual Studio with the Text File template selected and licenses.licx entered as the name.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-server/add-item-licenses-licx.a57aba29.webp)
2. Paste this line into the file and save it:

    ```text
    TXTextControl.ServerTextControl, TXTextControl.Server, Version=34.0.1900.500, Culture=neutral, PublicKeyToken=6b83fe9a75cfb638
    ```

The build compiles the license of `ServerTextControl` into your application from this line. The line must contain the full version of the referenced *TXTextControl.Server.dll*; after an upgrade, change it to the new version. See [License File Changes](https://docs.textcontrol.com/tx-text-control-dotnet-server/installation/upgrading/previous-versions/#license-file-changes).

### Write the Controller

Open *HomeController.cs* in the *Controllers* folder and replace its `Index()` method with the following method. The other actions of the template stay as they are.

```csharp
public ActionResult Index()
{
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        // Initialize the new instance before using it
        tx.Create();

        // Add static text in bold
        TXTextControl.Selection sel = new TXTextControl.Selection();
        sel.Text = "Welcome to Text Control\r\n";
        sel.Bold = true;

        tx.Selection = sel;

        // Add a merge field
        TXTextControl.DocumentServer.Fields.MergeField mergeField =
            new TXTextControl.DocumentServer.Fields.MergeField()
            {
                Text = "{{company}}",
                Name = "company",
                TextBefore = "Company name: "
            };

        tx.ApplicationFields.Add(mergeField.ApplicationField);

        // Merge the data into the merge field
        using (TXTextControl.DocumentServer.MailMerge mailMerge =
            new TXTextControl.DocumentServer.MailMerge())
        {
            mailMerge.TextComponent = tx;
            mailMerge.MergeJsonData("[{\"company\": \"Text Control, LLC\" }]");
        }

        // Save the result as HTML for the view
        string result = "";
        tx.Save(out result, TXTextControl.StringStreamType.HTMLFormat);

        ViewBag.Document = result;
    }

    return View();
}
```

An action of an ASP.NET MVC 5 controller returns `ActionResult`. The `IActionResult` interface of ASP.NET Core doesn't exist in MVC 5, and a method declared with it doesn't compile there.

#### What the Code Does

- [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/llms-full.txt) is the document processing component without a user interface. [Create](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) initializes it and must be called before any other member.
- A [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/selection/llms-full.txt) with [Text](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/selection/text-property/llms-full.txt) and [Bold](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/selection/bold-property/llms-full.txt) set is assigned to [ServerTextControl.Selection](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/selection-property/llms-full.txt), which inserts the bold line.
- A [MergeField](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver-fields/mergefield/llms-full.txt) is an MS Word MERGEFIELD. [Name](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver-fields/mergefield/name-property/llms-full.txt) is the name the data is matched by, [Text](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver-fields/mergefield/text-property/llms-full.txt) the text the field shows before the merge and [TextBefore](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver-fields/mergefield/textbefore-property/llms-full.txt) the text displayed before the field's text. [ApplicationFields.Add](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/applicationfieldcollection/add-method/llms-full.txt) inserts its [ApplicationField](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver-fields/fieldadapter/applicationfield-property/llms-full.txt) at the current input position.
- [MailMerge](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver/mailmerge/llms-full.txt) merges data into the document of its [TextComponent](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver/mailmerge/textcomponent-property/llms-full.txt). [MergeJsonData](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol-documentserver/mailmerge/mergejsondata-method/llms-full.txt) takes a JSON array with one object per record and replaces the field `company` with "Text Control, LLC".
- [Save](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/save-method/llms-full.txt) with [StringStreamType.HTMLFormat](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/stringstreamtype/enumeration/llms-full.txt) returns the document as an HTML string, which the action passes to the view in `ViewBag.Document`.

### Show the Result in the View

Open *Index.cshtml* in the *Views/Home* folder and replace its complete content with:

```cshtml
@{
    ViewData["Title"] = "Home Page";
}

@Html.Raw(ViewBag.Document)
```

`Html.Raw` writes the HTML string into the page without encoding it.

### Run the Application

Press **F5** to build and start the application. The home page shows the bold line "Welcome to Text Control" and below it "Company name: Text Control, LLC".

### Return a PDF Instead

To send the merged document as a PDF file, save it with [BinaryStreamType.AdobePDF](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/binarystreamtype/enumeration/llms-full.txt) and return it with the `File` method of the MVC 5 controller. Replace the lines from `// Save the result as HTML for the view` to the end of the `using` block with:

```csharp
        // Save the result as PDF and send it to the browser
        byte[] baPdf;
        tx.Save(out baPdf, TXTextControl.BinaryStreamType.AdobePDF);

        return File(baPdf, "application/pdf", "result.pdf");
    }
}
```

The final `return View();` of the method is then no longer reached and can be removed.

### Merge a Template From a File

Instead of creating the document in code, load a Word template that contains merge fields. Copy *template.docx* to the *App\_Data* folder of the project and replace the lines that create the selection and the merge field with:

```csharp
        // Load a DOCX template with MS Word merge fields
        TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings()
        {
            ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord
        };

        tx.Load(Server.MapPath("~/App_Data/template.docx"),
            TXTextControl.StreamType.WordprocessingML, ls);
```

[ApplicationFieldFormat](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/loadsettings/applicationfieldformat-property/llms-full.txt) set to `MSWord` imports the Word fields of the template as application fields; without it, the template has no fields to merge. [Merge Fields](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/mail-merge/fields/llms-full.txt) explains fields and templates in detail.

### Next Steps

- [ASP.NET MVC Document Viewer](https://docs.textcontrol.com/tx-text-control-dotnet-server/integration/dotnet-framework/mvc-viewer/llms-full.txt): show the merged documents in the browser.
- [Mail Merge and Reporting](https://docs.textcontrol.com/tx-text-control-dotnet-server/mail-merge/llms-full.txt): merge blocks, data sources and merge events.
- [.NET Framework Applications](https://docs.textcontrol.com/tx-text-control-dotnet-server/deployment/windows/dotnet-framework/llms-full.txt): the files to copy to the server.
- [From ASP.NET MVC on the .NET Framework to ASP.NET Core](https://docs.textcontrol.com/tx-text-control-dotnet-server/installation/upgrading/previous-versions/#from-asp-net-mvc-on-the-net-framework-to-asp-net-core): move the application to .NET.

---

## Nearby

- **Previous:** [ASP.NET MVC Document Viewer](https://docs.textcontrol.com/tx-text-control-dotnet-server/integration/dotnet-framework/mvc-viewer/llms-full.txt)
- **Next:** [ASP.NET Web Forms Document Editor](https://docs.textcontrol.com/tx-text-control-dotnet-server/integration/dotnet-framework/web-forms-editor/llms-full.txt)
