Getting Started: ServerTextControl and MailMerge with ASP.NET Core

The following tutorial shows how to create a .NET 6 ASP.NET Core web application that uses the ServerTextControl class and MailMerge to create documents.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\Documents\TX Text Control 32.0.NET Server for ASP.NET\CSharp\Core\mailmerge\

Creating the Application

Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 6 SDK.

1. In Visual Studio 2022, create a new project by choosing Create a new project.

2. Select ASP.NET Core Web App (Model-View-Controller) as the project template and confirm with Next.

3. Choose a name for your project and confirm with Next.

4. In the next dialog, choose .NET 6 (Long-term support) as the Framework and confirm with Create.

Image

Adding the NuGet Package

1. In the Solution Explorer, select your created project and choose Manage NuGet Packages... from the Project main menu.

Select Text Control Offline Packages from the Package source drop-down.

Install the latest version of the following package:

  • TXTextControl.TextControl.ASP.SDK

Image

Using ServerTextControl and MailMerge

1. Find the HomeController.cs file in the Controllers folder. Replace the Index() method with the following code:

public IActionResult Index() {

  using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) {
    tx.Create();

    // adding static text
    TXTextControl.Selection sel = new TXTextControl.Selection();
    sel.Text = "Welcome to Text Control
";
    sel.Bold = true;

    tx.Selection = sel;

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

    tx.ApplicationFields.Add(mergeField.ApplicationField);

    // alternatively load a template
    //TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings() {
    //    ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord
    //};

    //tx.Load("template.docx", TXTextControl.StreamType.WordprocessingML, ls);

    // merge fields with MailMerge engine
    using (TXTextControl.DocumentServer.MailMerge mailMerge =
      new TXTextControl.DocumentServer.MailMerge()) {
        mailMerge.TextComponent = tx;
        mailMerge.MergeJsonData("[{\"company\": \"Text Control, LLC\" }]");
      }

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

    // alternatively save as PDF
    //byte[] baPdf;
    //tx.Save(out baPdf, TXTextControl.BinaryStreamType.AdobePDF);

    ViewBag.Document = result;
  }

  return View();
}

Displaying the Results

1. Find the Index.cshtml file in the Views -> Home folder. Replace the complete file with the following code:

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

@Html.Raw(ViewBag.Document)

Compile and start the application.