New JavaScript API Calls for Typical MailMerge Tasks
This article shows how to use the improved JavaScript API for typical MailMerge tasks such as inserting merge blocks.

Version X18 (28.0) comes with a complete JavaScript API for the online document editor shipped with TX Text Control .NET Server.
The complete JavaScript API documentation can be found in our online docs:
This article describes typical tasks for MailMerge and reporting applications such as inserting merge fields, merge blocks and how to loop through these MailMerge elements.
Inserting Merge Fields
In the new API, each element, like a TextField, is accessible through it's own collection. The following code snippet shows how to insert a merge field, that is compatible with MailMerge, using the Application
The collection is accessible through the application
TXTextControl.applicationFields.add(
TXTextControl.ApplicationFieldFormat.MSWord,
"MERGEFIELD",
"FieldText",
["FieldName"],
af => {
var highlightColor = "rgba(9, 165, 2, 0.3)";
var highlightMode = TXTextControl.HighlightMode.Always;
af.setHighlightColor(highlightColor);
af.setHighlightMode(highlightMode);
}
);
The last parameter in the above call of the add method returns the inserted field in a callback. In this code snippet, this returned object is used to add additional properties that are not available in the constructor of the add method.
The above snippet shows how to insert merge fields (or any other supported type) into a document. The same can be achieved by using the add
var mergeField = new TXTextControl.MergeField();
mergeField.name = "company";
mergeField.text = "«company»";
mergeField.numericFormat = "$#,###.00"
TXTextControl.addMergeField(mergeField);
Looping Through Fields
The next code snippet is using the for
TXTextControl.applicationFields.forEach(function(appField) {
appField.getName(function(name) { console.log(name) });
})
The below code snippet changes the text of each field in the document:
TXTextControl.applicationFields.forEach(function(appField) {
appField.setText("new text");
})
Inserting Merge Blocks
A merge block that repeats content based on the hierarchical data using Mail
TXTextControl.selection.getStart(function(curSelStart) {
curSel.getLength(function(curSelLength) {
TXTextControl.subTextParts.add(
"txmb_myMergeBlock", 0, curSelStart + 1, curSelLength);
})
});
The Selection object is used to retrieve the start and length index of the current selection which is then used in the add method of the SubTextPartCollection.
The next code snippet shows how to iterate through all SubTextParts in order to return valid merge block names:
TXTextControl.subTextParts.forEach(function(subTextPart) {
subTextPart.getName(function(name) {
var mergeBlockName;
if (name.startsWith("txmb_")) {
mergeBlockName = name.substring(5, name.length);
console.log(mergeBlockName);
}
});
});
Read more about the improved JavaScript API in our documentation and download the trial version of TX Text Control .NET Server to test this on your own.
Also See
This post references the following in the documentation:
- Javascript: Application
Field Collection.add Method - Javascript: Application
Field Collection Object - Javascript: Collection.for
Each Method - Javascript: Formatted
Text.application Fields Property - Javascript: Selection Object
- Javascript: Sub
Text Part Collection.add Method - Javascript: TXText
Control.add Merge Field Method - TXText
Control. Document Server. Mail Merge Class
Angular
Integrate document processing, editing, sharing, collaboration, creation, electronic signatures, and PDF generation into your Angular Web applications.
Related Posts
Using the Document Editor in SPA Applications using the removeFromDom Method
This article shows how to use the removeFromDom method to remove the Document Editor from the DOM when it is no longer needed. This is useful when the Document Editor is used in a Single Page…
TXTextControl.Web: Creating a Custom ButtonBar using the InputFormat Class
TX Text Control X18 implements the InputFormat class in the JavaScript API that enables developers to build custom button bars.
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,…
Video Tutorial: Creating a MailMerge Template and JSON Data Structure
This video tutorial shows how to create a MailMerge template and a JSON data structure to merge data into a document. This tutorial will walk you through the steps necessary to create a template…
Observe When the Reporting Preview Tab is Active Using MutationObserver
This article shows how to observe when the Reporting Preview tab is active using MutationObserver. The Reporting Preview tab is a feature of the TX Text Control Document Editor that allows you to…