The MailMerge TX Text Control .NET Server for ASP.NET
DocumentServer Namespace
MailMerge Class
The MailMerge class is a .NET component that can be used to effortlessly merge template documents with database content in .NET projects, such as ASP.NET web applications, web services or Windows services.
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.

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>
view raw test.html hosted with ❤ by GitHub

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);
}
}
view raw ext.cs hosted with ❤ by GitHub

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);
view raw test.cs hosted with ❤ by GitHub

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 MergeField TX Text Control .NET Server for ASP.NET
DocumentServer.Fields Namespace
MergeField Class
The MergeField class implements the MS Word specific MERGEFIELD field.
based on the selected text. After the text is removed, the newly created merge fields is then inserted into the document at the current input position:

// 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);
}
view raw test.cs hosted with ❤ by GitHub

After all placeholders have been converted to merge fields, the template can be used like any other MailMerge template to create documents.

Converted placeholders