Products Technologies Demo Docs Blog Support Company

ASP.NET Core: Server-Side MailMerge

TXTextControl.Web is available as an ASP.NET Core component that can be used in ASP.NET Core Web Applications. This article shows how to use a model as the data excerpt file and to merge the created template server-side.

ASP.NET Core: Server-Side MailMerge

With the latest multi-platform release of the NuGet package, TXTextControl.Web can now be used in ASP.NET Core Web Applications. This article shows how to use a model as the data excerpt file for the document editor and how to merge the actual data into the created template in an HTTP Post method in the MVC controller.

Loading Data from Model

Consider the following, very simple model Customer.cs:

public class Customer
{
    public string Name { get; set; }
    public string Firstname { get; set; }
    public string Company { get; set; }
}

In the controller, a dummy data object is created and stored in a list to be used as the view model.

public IActionResult Index()
{
    List<Customer> lDummyData = new List<Customer>();

    lDummyData.Add(new Customer()
        {
            Company = "Text Control, LLC",
            Firstname = "Tim",
            Name = "Typer"
        });

    return View(lDummyData);
}

In the view itself, the excerpt file is loaded from a model using the LoadDataFromModel method.

@using TXTextControl.Web
@using TXTextControl.Web.MVC

@model List<TXCoreBackend.Models.Customer>

<div class="row">

    <div class="col-lg-12 editor">
        @Html.TXTextControl().TextControl(settings =>
        {
            settings.Dock = DockStyle.Fill;
        }).LoadDataFromModel(Model).Render()
    </div>

</div>

Based on the data in this model, the reporting tab merge field drop-downs are filled with available merge field names:

Data model in TX Text Control

Server-Side Merge

On clicking the button Merge Document Server-Side, the document is saved using saveDocument, encapsulated in an object and posted to the HTTP Post controller method MergeDocument:

function mergeDocument() {

    $("#infobox").text("Merging document...");

    TXTextControl.saveDocument(
        TXTextControl.StreamType.InternalUnicodeFormat, function (e) {

        $.ajax({
            type: "POST",
            url: "/Home/MergeDocument",
            contentType: "application/json",
            data: JSON.stringify({
                Document: e.data
            }),
            success: function (content) {
                TXTextControl.loadDocument(
                    TXTextControl.StreamType.InternalUnicodeFormat, 
                    content, 
                    function () {
                        $("#infobox").text("Merged and loaded");
                    });
            }
        })

    })
}

In the controller method, the template is loaded into a ServerTextControl instance, merged with data using MailMerge, saved back and returned to the client.

[HttpPost]
public string MergeDocument([FromBody] MergeDocument MergeDocument)
{
    // create some merge data rows
    List<Customer> lActualData = new List<Customer>() { 
        new Customer()
        {
            Company = "Text Control, LLC",
            Firstname = "Tim",
            Name = "Typer"
        },
        new Customer()
        {
            Company = "Text Control GmbH",
            Firstname = "Bernie",
            Name = "Bold"
        }
    };

    byte[] baResults;

    // create a new ServerTextControl instance to connect to MailMerge
    using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl())
    {
        tx.Create();

        // load the template
        tx.Load(Convert.FromBase64String(MergeDocument.Document),
            TXTextControl.BinaryStreamType.InternalUnicodeFormat);

        // connect MailMerge
        MailMerge mm = new MailMerge();
        mm.TextComponent = tx;

        // merge data into template
        mm.MergeObjects(lActualData);

        // save results
        tx.Save(out baResults, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
    }

    return Convert.ToBase64String(baResults);
}

Loading the Results

After a successful merge process, the returned document is loaded into the document editor:

Data model in TX Text Control

TX Text Control .NET Server can be used to create ASP.NET Core Web Applications including the fully-featured document editor and the backend functionality including MailMerge capabilities.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Also See

This post references the following in the documentation:

  • Javascript: TXTextControl.saveDocument Method
  • TXTextControl.DocumentServer.MailMerge Class
  • TXTextControl.ServerTextControl Class
  • TXTextControl.Web.MVC Namespace
  • TXTextControl.Web.MVC.TextControl.LoadDataFromModel Method

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.NETBlazorASP.NET Core

TX Text Control for Blazor: Mail Merge Integration Tutorial

This tutorial shows how to integrate the TX Text Control MailMerge component into a Blazor application using the TX Text Control .NET Server.


ASP.NETDocument Editor.NET Core

Creating an ASP.NET Core Web Application using TXTextControl.Web

This article shows how to create a ASP.NET Core Web Application using the document editor.


ASP.NETDocument Editor.NET Core

TXTextControl.Web for ASP.NET Core Web Applications Released

We just released the .NET Core client-side library of the TX Text Control Online Document Editor.


ASP.NETASP.NET CoreDocument Editor

Getting Started Video Tutorial: Document Editor in ASP.NET Core C# on Linux

This video tutorial shows how to use the Document Editor in an ASP.NET Core application using C# and deploy on Linux using Docker. This tutorial is part of the TX Text Control Getting Started…


ASP.NETDocument EditorDocument Protection

Document Protection in ASP.NET with TX Text Control: Healthcare Use Cases

This article explores document protection use cases in the healthcare domain. It also explains how to assign usernames, configure edit modes, and use editable regions based on user roles using the…