MailMerge with Angular and ASP.NET Core
This article shows how to use TX Text Control .NET Server in an Angular application to create a mail merge application. The backend is implemented using an ASP.NET Core Web API to merge templates with JSON data.

In many cases, the Angular version of the document editor is used in combination with an ASP.NET Core backend. The backend provides the data source for the merge process and also provides the endpoints to perform the actual document merge with the created template and live data.
In this example, an endpoint is implemented in the backend to return a JSON string that is used to populate the drop-downs that list the available fields and merge blocks.
Filling the Drop-Down Menus
The GetJsonData endpoint does nothing more than load a JSON text file and return it.
[HttpGet]
[Route("GetJsonData")]
public MailMergeData GetJsonData()
{
var mailMergeData = new MailMergeData();
string json = System.IO.File.ReadAllText("App_Data/data.json");
mailMergeData.Json = json;
return mailMergeData;
}
The MailMergeData class is implemented as an exchange object for all communication between the client and the backend endpoints.
public class MailMergeData
{
public string? Json { get; set; }
public string? Template { get; set; }
public string? Document { get; set; }
}
In the Angular application, the endpoint is called in the ngOnInit method. When the Document Editor is loaded, the loadJsonData method is called with the loaded JSON string.
@HostListener('document:txDocumentEditorLoaded', ['$event'])
onTxDocumentEditorLoaded() {
// wait until TXTextControl has been loaded
TXTextControl.addEventListener("textControlLoaded", () => {
TXTextControl.loadJsonData(this.mailMergeData.json);
});
}
ngOnInit() {
this.getJsonData();
}
getJsonData() {
this.http.get<MailMergeData>('/mailmerge/getjsondata').subscribe(
(result) => {
this.mailMergeData = result;
}
);
}
The same exchange object is implemented as an interface in TypeScript.
interface MailMergeData {
json?: string;
template?: string;
document?: string;
}
Merging the Template
Once the user has completed the template, it can be merged server-side using the MergeTemplate Web API endpoint.
[HttpPost]
[Route("MergeTemplate")]
public MailMergeData MergeTemplate([FromBody] MailMergeData mailMergeData)
{
using (var tx = new TXTextControl.ServerTextControl())
{
tx.Create();
byte[] templateData = Convert.FromBase64String(mailMergeData.Template);
tx.Load(templateData, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
var merge = new MailMerge
{
TextComponent = tx
};
merge.MergeJsonData(mailMergeData.Json);
byte[] documentData;
tx.Save(out documentData, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
mailMergeData.Document = Convert.ToBase64String(documentData);
return mailMergeData;
}
}
Clicking the Merge Template button saves the document in internal TX Text Control format and sends it to the Web API endpoint in an http post method.
mergeTemplate() {
TXTextControl.saveDocument(TXTextControl.StreamType.InternalUnicodeFormat, (document: any) => {
this.mailMergeData.template = document.data;
// send a post request to the server
this.http.post('/mailmerge/mergetemplate', this.mailMergeData).subscribe(
(result) => {
this.mailMergeData = result;
TXTextControl.loadDocument(
TXTextControl.StreamType.InternalUnicodeFormat,
this.mailMergeData.document);
}
);
});
}
The server returns the final document, which can be loaded into the Document Editor using loadDocument.
This project is available for you to download and test on your own from our GitHub repository.
Conclusion
Using the Angular version of the Document Editor in combination with an ASP.NET Core backend is a powerful combination to create a mail merge application. The backend provides the data source for the merge process and also provides the endpoints to perform the actual document merge with the created template and live data.
Download and Fork This Sample on GitHub
We proudly host our sample code on github.com/TextControl.
Please fork and contribute.
Requirements for this sample
- TX Text Control .NET Server
- Visual Studio 2022
Jump to the other posts in this series:
- MailMerge with Angular and ASP.NET Core
- How to Add Electronic and Digital Signatures to PDFs in ASP.NET Core C# and Angular
- Document Viewer for Angular: SignatureSettings Explained
- Asynchronous Loading and Saving in Angular Document Editor with an ASP.NET Core Web API
- Preparing Forms for the Document Viewer and Customizing the Signing Process in Angular and ASP.NET Core
- Handling Form Field Data in Angular Applications
Angular
Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.
Related Posts
Reuse Persistent Document Editor Components in Angular SPA Applications
An Angular Single Page Application (SPA) dynamically updates the web page without reloading by loading all necessary resources once. This article demonstrates how to reuse persistent Document…
Building an ASP.NET Core Backend (Linux and Windows) for the Document Editor…
This article shows how to create a backend for the Document Editor and Viewer using ASP.NET Core. The backend can be hosted on Windows and Linux and can be used in Blazor, Angular, JavaScript, and…
AngularJavaScriptDocument Editor
Getting Started: Document Editor Version 33.0 with Angular CLI 19.0
This article shows how to use the TX Text Control Document Editor version 33.0 npm package for Angular within an Angular CLI 19.0 application. It uses the trial backend running on our servers, but…
AngularASP.NET CoreDocument Editor
Changing the Language in the Angular Document Editor Using the Resource Kit
This article walks you through the process of building a satellite assembly using the Resource Kit to customize the language of the TX Text Control interface in the Angular Document Editor.
Creating Advanced Tables in PDF and DOCX Documents with C#
This article shows how to create advanced tables in PDF and DOCX documents using the TX Text Control .NET for ASP.NET Server component. This article shows how to create tables from scratch,…