Toggle Field Codes in TX Text Control
TX Text Control provides the ability to insert visual merge fields into a document. This example shows how to toggle between the visual representation of merge fields and the actual field codes. This article shows how to toggle field codes in TX Text Control.

In TX Text Control, a merge field is a type of placeholder or special field within a document that is used to insert dynamic data during a document merge process. Merge fields are typically used in document automation scenarios, such as creating personalized letters, invoices, or reports, where specific pieces of data - such as names, addresses, dates, or other variable information - are dynamically inserted into the document.
Merge fields that can be automatically merged with data such as JSON or XML using the Mail
In previous versions of the Editor ribbon UI, we had a button to toggle between the actual human-readable field text and the actual field codes - a feature that is sort of hidden in MS Word. This feature confused many end users because most of them don't really know what field codes are, and they shouldn't.
TX Text Control is popular because non-technical users can create their reporting or automation templates using only MS Word. So in later versions we decided to remove this button from the UI. But in some cases, for power users, it might be helpful to toggle between these states.
Inserting Merge Fields
Consider the following code, which adds a new merge field to a document.
MergeField mergeField = new MergeField();
mergeField.Name = "MyMergeField";
mergeField.NumericFormat = "0.00";
mergeField.DateTimeFormat = "dd.MM.yyyy";
mergeField.TextBefore = "before";
mergeField.TextAfter = "after";
mergeField.Text = "«" + mergeField.Name + "»";
mergeField.ApplicationField.HighlightMode = HighlightMode.Activated;
textControl1.ApplicationFields.Add(mergeField.ApplicationField);
The text of the field is displayed in the document.
Toggling Merge Fields
The following code can use the Parameters array to generate the field code string that will be used as the new field text when toggling.
private void ToggleFieldCodes(ApplicationField applicationField)
{
// Toggle the field codes of an ApplicationField
if (applicationField.Text.StartsWith("{ " + applicationField.TypeName))
{
// Reapply the stored field text
applicationField.Text = applicationField.Name;
}
else
{
// Store the current field text
applicationField.Name = applicationField.Text;
var fieldTextBuilder = new StringBuilder("{ " + applicationField.TypeName + " ");
fieldTextBuilder.Append(string.Join(" ", applicationField.Parameters.Select(p => p.Replace("\"", ""))));
fieldTextBuilder.Append(" }");
applicationField.Text = fieldTextBuilder.ToString();
}
}
When the field is toggled, the field code is displayed in the document.
JavaScript Version
The same task can be accomplished in the online editor with the following JavaScript code:
function processFieldCode(appField) {
appField.getText(text => {
appField.getName(name => {
appField.getTypeName(typeName => {
if (text.startsWith(`{ ${typeName}`)) {
appField.setText(`«${name}»`);
} else {
appField.getParameters(params => {
const paramString = `{ ${typeName} ${params.join(" ")} }`;
appField.setText(paramString);
});
}
});
});
});
}
function toggleFieldCodes() {
TXTextControl.applicationFields.getItem(processFieldCode);
}
Conclusion
TX Text Control provides an easy way to insert merge fields into a document and toggle between field text and field code. This feature is useful for power users who need to see the field code for debugging or troubleshooting purposes.
Related Posts
TX Text Control 33.0 SP3 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 3 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
TX Text Control 33.0 SP2 is Now Available: What's New in the Latest Version
TX Text Control 33.0 Service Pack 2 is now available, offering important updates and bug fixes for all platforms. If you use TX Text Control in your document processing applications, this service…
Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format
Maintaining the integrity and functionality of documents throughout their lifecycle is paramount. TX Text Control provides a robust ecosystem that focuses on preserving documents in their internal…
Expert Implementation Services for Legacy System Modernization
We are happy to officially announce our partnership with Quality Bytes, a specialized integration company with extensive experience in modernizing legacy systems with TX Text Control technologies.
Service Pack Releases: What's New in TX Text Control 33.0 SP1 and 32.0 SP5
TX Text Control 33.0 Service Pack 1 and TX Text Control 32.0 Service Pack 5 have been released, providing important updates and bug fixes across platforms. These service packs improve the…