Converting HTML Placeholders to Merge Fields
The MailMerge class of TX Text Control uses MS Word compatible merge fields to populate data from various data sources into templates. In order to use legacy templates from other formats, such as HTML, placeholders can be converted into merge fields.

The Mail
A typical legacy HTML template contains textual placeholders like in the following HTML:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p>This is a sample text with #textfields#.</p>
<p>More #textmarkers# here.</p>
</body>
</html>
In order to use the full power of Text Control's reporting engine, those templates should be converted into the TX Text Control internal format or an MS Word format such as Office Open XML (DOCX).
With TX Text Control's API, it is possible to load these HTML documents, find these placeholders and convert them into merge fields that can be directly used with MailMerge.
In order to find the placeholders like in the sample template above, the following extension method can be used to find them using a simple regular expression:
public static class MyExtensions {
public static MatchCollection Find(
this TXTextControl.TextControl tx,
string pattern,
RegexOptions regexOptions) {
// replace the CRLFs with LFs to be compatible with
// TextControl's index
var sText = tx.Text.Replace("\r\n", "\n");
// new RegEx and return the matches
Regex rg = new Regex(pattern, regexOptions);
return rg.Matches(sText);
}
}
The extension method extends the Find method of TX Text Control to search for matches based on a given regular expression pattern:
MatchCollection matchedFields = textControl1.Find(@"#(.*)#", RegexOptions.None);
In our sample, the template placeholder format is:
#placeholder#
The extension Find method returns a MatchCollection that is then used to select the text in order to create a new Merge
// load a document
textControl1.Load("sample.html", TXTextControl.StreamType.HTMLFormat);
// create a MatchCollection
MatchCollection matchedFields = textControl1.Find(@"#(.*)#", RegexOptions.None);
// loop through all matches
foreach (Match match in matchedFields) {
// select the text
textControl1.Select(match.Index, match.Length);
// create a new field based on the text
TXTextControl.DocumentServer.Fields.MergeField mergeField =
new TXTextControl.DocumentServer.Fields.MergeField() {
Text = textControl1.Selection.Text,
Name = textControl1.Selection.Text.Trim('#')
};
// enable highlighting and the second input position
mergeField.ApplicationField.DoubledInputPosition = true;
mergeField.ApplicationField.HighlightMode = TXTextControl.HighlightMode.Activated;
// remove the text
textControl1.Selection.Text = "";
// insert the field at the current input position
textControl1.ApplicationFields.Add(mergeField.ApplicationField);
}
After all placeholders have been converted to merge fields, the template can be used like any other MailMerge template to create documents.
ASP.NET
Integrate document processing into your applications to create documents such as PDFs and MS Word documents, including client-side document editing, viewing, and electronic signatures.
- Angular
- Blazor
- React
- JavaScript
- ASP.NET MVC, ASP.NET Core, and WebForms
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…