Products Technologies Demo Docs Blog Support Company

ServerTextControl and MailMerge Classes in Windows Forms

Did you already know that the ServerTextControl and MailMerge classes can be used in visible Windows Forms applications with the X9 Professional client license? This is an additional feature we added to the client versions without optional server and run-time licences. ServerTextControl can be used for all processes in the background where no GUI is required. If you need to prepare a document programmatically before it is displayed to the end-user, you can use an instance of…

ServerTextControl and MailMerge Classes in Windows Forms

Did you already know that the ServerTextControl and MailMerge classes can be used in visible Windows Forms applications with the X9 Professional client license?

This is an additional feature we added to the client versions without optional server and run-time licences.

ServerTextControl can be used for all processes in the background where no GUI is required. If you need to prepare a document programmatically before it is displayed to the end-user, you can use an instance of TXTextControl.ServerTextControl to create the document with the same API just like with the Windows Forms control TXTextControl.TextControl.

Advantages of using ServerTextControl:

  1. Better performance
  2. Must not be visible on a form
  3. No visual update

In order to use the ServerTextControl, you'll need to add a reference to the TXTextControl.Server.dll and an entry in the licenses.licx file:

TXTextControl.ServerTextControl, TXTextControl.Server, Culture=neutral, PublicKeyToken=6b83fe9a75cfb638

The following code shows how to create an instance of ServerTextControl. An using statement is recommended as the object must be disposed explicitly.

using (TXTextControl.ServerTextControl serverTextControl1 
       = new TXTextControl.ServerTextControl()) 
{ 
    serverTextControl1.Create(); 
    ... 
}

Using MailMerge in Windows Forms Applications

Not only the ServerTextControl class can be used with the client license in Windows Forms or WPF applications. The MailMerge component can be connected to a visible TextControl as well.

The following tutorial shows how to connect MailMerge with TextControl, to load a template and to merge data.

Open Visual Studio and find the TextControl icon in the toolbox tab TX Text Control 19.0. Drag and drop an instance of TextControl to the form, click the SmartTag in the upper right corner and choose Add a Button Bar, Add a Status Bar, Add a Ruler, Add a Vertical Ruler and finally Arrange Controls Automatically to connect all controls.

Using MailMerge in Windows Forms applications

Now, find the MailMerge component in the toolbox and drag and drop an instance onto the form:

Using MailMerge in Windows Forms applications

Select the MailMerge component to show the Properties and pick the TextControl from the drop-down box of the TextComponent property.

Using MailMerge in Windows Forms applications

In the next step, we will create a simple template using TX Text Control Words. Open the SmartTag again and choose "Start TXTextControlWords".

Choose New from the File main menu to start with a blank document.

Open the Mailings tab which contains all functionality to add a data source, text fields or repeating blocks. For this tutorial, we won't add a data source, but insert fields with a specific name directly.

Click on the Insert Merge Field button to open the Merge Field Options dialog:

Using MailMerge in Windows Forms applications

Type in the field name company and confirm with OK. Repeat this step with additional fields named "name" and "firstname". You can add additional text and formatting, so that your resulting template looks like this:

Using MailMerge in Windows Forms applications

Save the document as a new DOCX document in the directory of your newly created Visual Studio project.

Switch back to Visual Studio, add a MenuStrip to the form and add a menu item called Merge. Using MailMerge in Windows Forms applications

Double click the menu item to open the event handler in order to add the following code:

// load template
TXTextControl.LoadSettings ls = new TXTextControl.LoadSettings();
ls.ApplicationFieldFormat = TXTextControl.ApplicationFieldFormat.MSWord;
textControl1.Load("template.docx",
    TXTextControl.StreamType.WordprocessingML, ls);

// create a dummy data source
DataTable dt = new DataTable();

dt.Columns.Add("company");
dt.Columns.Add("firstname");
dt.Columns.Add("name");

dt.Rows.Add(new object[] { "Facebook", "Mark", "Zuckerberg" });
dt.Rows.Add(new object[] { "Microsoft", "Bill", "Gates" });
dt.Rows.Add(new object[] { "Microsoft", "Steve", "Ballmer" });
dt.Rows.Add(new object[] { "Oracle", "Larry", "Ellison" });

// merge template
mailMerge1.Merge(dt, true);

Compile and start the application in order to click the menu item. The merged document is loaded back into the connected TextControl instance automatically and the document should have 4 pages now:

Using MailMerge in Windows Forms applications

Stay in the loop!

Subscribe to the newsletter to receive the latest updates.

Related Posts

Windows FormsGetting StartedTutorial

Windows Forms Tutorial: Create Your First Windows Forms C# Application

This tutorial shows how to create your first Windows Forms application with C# using TX Text Control .NET for Windows Forms in Visual Studio 2022.


ASP.NETASP.NET CoreMailMerge

How to Mail Merge MS Word DOCX Documents in ASP.NET Core C#

Mail merge is the process of merging data, such as Json or IEnumerable objects, into a template document, such as a DOC or DOCX file. This tutorial is a walkthrough of the steps necessary to…


AngularDocument EditorNode.js

Creating an Angular Document Editor Application with a Node.js WebSocket Server

This tutorial shows how to create an Angular application that uses the Document Editor with a Node.js WebSocket server.


ASP.NETASP.NET CoreMailMerge

Adding SVG Watermarks to Documents

This article shows how to add SVG images to document section headers that repeat automatically on each page. This watermark will be inserted vertically and horizontally centered on each section page.


ASP.NETASP.NET CoreMailMerge

Using MailMerge in ASP.NET Core 6 Web Applications

This article shows how to use the TX Text Control ASP.NET MailMerge class to merge templates with JSON data within a .NET 6 application in Visual Studio 2022.