Products Technologies Demo Docs Blog Support Company

MailMerge: Using IncludeText Fields to Inject Formatted Text

Generally, IncludeText fields contain a reference to external documents that will be included during the mail merge process. That allows you to store a specific sub-template separately in order to reuse it in other templates. But these fields can do much more. This article shows how to use the IncludeTextMerging event to inject dynamic formatted content. In this case, the Filename parameter of the IncludeText field can be used to save a specific identifier like an ID or a field name. Based…

MailMerge: Using IncludeText Fields to Inject Formatted Text

Generally, IncludeText fields contain a reference to external documents that will be included during the mail merge process. That allows you to store a specific sub-template separately in order to reuse it in other templates.

But these fields can do much more. This article shows how to use the IncludeTextMerging event to inject dynamic formatted content. In this case, the Filename parameter of the IncludeText field can be used to save a specific identifier like an ID or a field name. Based on this value, we can decide which document or text should be inserted. This approach keeps the sub-templates more flexible as we do not need to store the actual name of each sub-template nor do we need to save the sub-templates physically.

Our sample template just contains two IncludeText fields and some text:

Template

In the following code, this template is loaded, the IncludeTextMerging event is attached and merged. As we do not have any normal merge fields in the template, we can call the Merge method without a DataTable. Instead of passing data, null is passed.

mailMerge1.LoadTemplate("template.docx", TXTextControl.DocumentServer.FileFormat.WordprocessingML);
mailMerge1.IncludeTextMerging += new TXTextControl.DocumentServer.MailMerge.IncludeTextMergingHandler(
                                 mailMerge1_IncludeTextMerging);
mailMerge1.Merge(null, false);
mailMerge1.SaveDocument("results.pdf", TXTextControl.StreamType.AdobePDF,
                        new TXTextControl.SaveSettings());

Now the interesting part. In the IncludeTextMerging event, a new ServerTextControl instance is created to provide the formatted text. At this point, it is also possible to load a sub-template from a database or a physical file.

The event arguments return the Filename property of the IncludeText field which is used as an identifier in our scenario. A simple switch statement shows how to handle this argument. Based on the field's Filename value, different text is added to the temporary ServerTextControl. After some formatting operations, the content is saved in a byte array in the internal TX Text Control format. This byte array can be passed to the IncludeTextDocument property of the IncludeTextEvent arguments. The complete document will be inserted at the current field location.

void mailMerge1_IncludeTextMerging(object sender,
                                   TXTextControl.DocumentServer.MailMerge.IncludeTextMergingEventArgs e)
{
    TXTextControl.ServerTextControl tempTX = new TXTextControl.ServerTextControl();
    tempTX.Create();

    switch (e.Filename)
    {
        case "\field1":
            tempTX.Text = "Field 1 inserted at " + DateTime.Now.ToString();
            tempTX.Select(0, 7);
            tempTX.Selection.Bold = true;
            break;

        case "\field2":
            tempTX.Text = "Field 2 inserted at " + DateTime.Now.ToString();
            tempTX.Select(0, 7);
            tempTX.Selection.Italic = true;
            break;
    }

  byte[] data;

    tempTX.Save(out data, TXTextControl.BinaryStreamType.InternalUnicodeFormat);
    tempTX.Dispose();

    e.IncludeTextDocument = data;
}

Using this approach, you can insert complete documents or just dynamic formatted field text which is not possible using the normal merge fields. The idea of the merge field is that the formatting is part of the template and the merge process just replaces the text. The following screenshot shows the resultsing PDF document with the injected formatted text.

Document

Download sample project

Feel free to post your questions or comments on this.

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

ASP.NETBlazorASP.NET Core

TX Text Control for Blazor: Mail Merge Integration Tutorial

This tutorial shows how to integrate the TX Text Control MailMerge component into a Blazor application using the TX Text Control .NET Server.


ASP.NETASP.NET CoreMail Merge

Mail Merge: Skipping Records During the Merge Process in .NET C#

This article shows how to skip records during the merge process in .NET C# using the MailMerge class of the Text Control Reporting engine.


ASP.NETASP.NET CoreMail Merge

Mail Merge MS Word DOCX Documents and Convert to PDF in .NET C#

This article shows how to merge data into MS Word DOCX documents and convert them to PDF using TX Text Control .NET Server.


ASP.NETASP.NET CoreMail Merge

Merging Self-Calculating Business Objects with TX Text Control MailMerge in C#

This article shows how to merge self-calculating business objects with TX Text Control MailMerge in C#. The sample shows how to merge a list of products with a total sum.


ASP.NETDigital SignatureDocument Viewer

Use Case: Create, Deploy and Process Insurance Claim Forms

Digital forms processing with electronic signatures offers several benefits that streamline and enhance administrative tasks. This article outlines a typical use case of an insurance claim form…