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()) 

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;
    TXTextControl.StreamType.WordprocessingML, ls);

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


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