This tutorial is a walkthrough of the steps necessary to create an ASP.NET Core MVC application. Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file.
The following tutorial shows how to create a simple .NET 6 C# Console App to convert MS Word documents to PDF that uses the Server ╰ TX Text Control .NET Server for ASP.NET
╰ TXTextControl Namespace
╰ ServerTextControl Class
The ServerTextControl class implements a component that provide high-level text processing features for server-based applications. class.
Creating the Application
Make sure that you downloaded the latest version of Visual Studio 2022 that comes with the .NET 6 SDK.
Prerequisites
The following tutorial requires a trial version of TX Text Control .NET Server for ASP.NET.
-
In Visual Studio 2022, create a new project by choosing Create a new project.
-
Select ASP.NET Core Web App (Model-View-Controller) as the project template and confirm with Next.
-
Choose a name for your project and confirm with Next.
-
In the next dialog, choose .NET 6 (Long-term support) as the Framework and confirm with Create.
Adding the NuGet Package
-
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 versions of the following package:
- TXTextControl.TextControl.ASP.SDK
Adding the Template and Data
-
In the Solution Explorer, select the project and click New Folder from the Project main menu and name it App_Data.
-
Download the following sample template and save it in the newly created folder:
-
In the same folder, create a new JSON file, name it data.json and add the following content to it:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters[ { "name": "Jackson", "firstname": "Peter", "city": "Charlotte", "zip": "28226", "street": "6920 Shannon Willow Rd", "product": [ { "name": "TX Text Control .NET Server for ASP.NET", "price": "2998" }, { "name": "TX Text Control .NET for Windows Forms", "price": "1459" } ] } ]
Adding a Web API Endpoint
-
Open the HomeController.cs and add the following method:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters[HttpGet] public IActionResult MergeDocument() { // create a ServerTextControl using (TXTextControl.ServerTextControl tx = new TXTextControl.ServerTextControl()) { tx.Create(); // load the template tx.Load("App_Data/template.tx", TXTextControl.StreamType.InternalUnicodeFormat); // create the mail merge engine using (TXTextControl.DocumentServer.MailMerge mm = new TXTextControl.DocumentServer.MailMerge()) { // connect to ServerTextControl instance mm.TextComponent = tx; // merge data var jsonData = System.IO.File.ReadAllText("App_Data/data.json"); mm.MergeJsonData(jsonData); } byte[] bDocument = null; // save in the internal format tx.Save(out bDocument, TXTextControl.BinaryStreamType.AdobePDF); // create memory stream from byte array MemoryStream stream = new MemoryStream(bDocument); stream.Position = 0; //Download Word document in the browser return File( stream, "application/pdf", "results.pdf"); } }
Calling the Web API
-
Open the Views -> Home -> Index.cshtml and replace the code with the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters@{ Html.BeginForm("MergeDocument", "Home", FormMethod.Get); { <div> <input type="submit" value="Merge Document" /> </div> } Html.EndForm(); }
Now, compile and start the application. Click the button Merge Document to call the Web API and to download the merged document.