
The source code is contained in the following directory:
Used TX Text Control controls:
Relevant API links:
A valid template can be created with the methods of the Windows Forms component TextControl. This enables developers to create their own template designer application similar to the one shipped.
A template consists of merge fields which can be merged with database content. The format of the field is maintained during the merge process. These fields are of the type ApplicationField where it's ApplicationField.TypeName must be MERGEFIELD.
The following code inserts a field with a specific name returned from a dialog box:
[C#]
if (NameDialog.ShowDialog() == DialogResult.OK)
{
TXTextControl.ApplicationField newField = new TXTextControl.ApplicationField(
TXTextControl.ApplicationFieldFormat.MSWord,
"MERGEFIELD", "<" + NameDialog.FieldName + ">",
new string[] { NameDialog.FieldName });
newField.ShowActivated = true;
newField.DoubledInputPosition = true;
textControl1.ApplicationFields.Add(newField);
}
[Visual Basic]
If NameDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim newField As New TXTextControl.ApplicationField( _
TXTextControl.ApplicationFieldFormat.MSWord, _
"MERGEFIELD", "<" + NameDialog.FieldName + ">", _
New String() {NameDialog.FieldName})
newField.ShowActivated = True
newField.DoubledInputPosition = True
TextControl1.ApplicationFields.Add(newField)
End If
Additionally, a template can contain repeating blocks. These blocks consist of two successive document targets with a specific name. The format of these targets is explained here.
The following code snippet checks whether the targets can be inserted at the beginning and end of the current selection and inserts them:
[C#]
if (NameDialog.ShowDialog() == DialogResult.OK)
{
string ERROR_INSERT = "Targets could not be inserted at the current selection.";
int startIndex = textControl1.Selection.Start;
int endIndex = textControl1.Selection.Start + textControl1.Selection.Length;
TXTextControl.DocumentTarget startTarget = new TXTextControl.DocumentTarget("blockstart_" + NameDialog.FieldName);
TXTextControl.DocumentTarget endTarget = new TXTextControl.DocumentTarget("blockend_" + NameDialog.FieldName);
textControl1.InputPosition = new TXTextControl.InputPosition(startIndex);
if (!textControl1.DocumentTargets.Add(startTarget))
{
MessageBox.Show(ERROR_INSERT);
return;
}
textControl1.InputPosition = new TXTextControl.InputPosition(endIndex);
if (!textControl1.DocumentTargets.Add(endTarget))
{
MessageBox.Show(ERROR_INSERT);
textControl1.DocumentTargets.Remove(startTarget);
return;
}
}
[Visual Basic]
If NameDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim ERROR_INSERT As String = "Targets could not be inserted at the current selection."
Dim startIndex As Integer = TextControl1.Selection.Start
Dim endIndex As Integer = TextControl1.Selection.Start + TextControl1.Selection.Length
Dim startTarget As New TXTextControl.DocumentTarget("blockstart_" + NameDialog.FieldName)
Dim endTarget As New TXTextControl.DocumentTarget("blockend_" + NameDialog.FieldName)
TextControl1.InputPosition = New TXTextControl.InputPosition(startIndex)
If Not TextControl1.DocumentTargets.Add(startTarget) Then
MessageBox.Show(ERROR_INSERT)
Return
End If
TextControl1.InputPosition = New TXTextControl.InputPosition(endIndex)
If Not TextControl1.DocumentTargets.Add(endTarget) Then
MessageBox.Show(ERROR_INSERT)
TextControl1.DocumentTargets.Remove(startTarget)
Return
End If
End If
The created documents must be saved in the supported formats of the DocumentServer.MailMerge component to maintain these merge fields.