# Using ServerTextControl in Desktop Applications

> Use ServerTextControl beside the Windows Forms editor: create and dispose it, pass documents as byte arrays, merge letters and license it in a class library.

- **Product:** TX Text Control .NET for Windows Forms
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for Windows Forms > Document Processing > Using ServerTextControl in Desktop Applications
- **2351 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/servertextcontrol/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/servertextcontrol/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/servertextcontrol/llms-full.txt

---

The `TextControl` on a form is the document the user works on. Everything else your application does with documents, such as merging letters, converting a folder to PDF or preparing a document before the user sees it, belongs in a `ServerTextControl`: the same document engine and programming interface, without a window. Used this way, the editor keeps its document and its undo history, and long work can run on a background thread while the form stays responsive.

[ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/components/servertextcontrol/llms-full.txt) in the Overview introduces the component. The examples below are handlers in *Form1.cs* with the `TextControl` named `textControl1`, and *Form1.cs* needs these `using` directives:

```csharp
using System.IO;
using TXTextControl;
```

### When to Use ServerTextControl

| Task | Use |
|---|---|
| The user edits, formats or reads a document | `TextControl` |
| Build or merge a document before the user sees it | `ServerTextControl`, then load the result into the `TextControl` |
| Convert, merge or print files the user doesn't see | `ServerTextControl` |
| Work that takes longer than a moment | `ServerTextControl` on a background thread |

A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/llms-full.txt) is a component without a window that has the document members of the `TextControl`; [What ServerTextControl Is](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/components/servertextcontrol/#what-servertextcontrol-is) describes the class and why it is faster and smaller for the same work.

### Create and Dispose Each Instance

Every use of a `ServerTextControl` follows the same pattern:

```csharp
private void createDocumentButton_Click(object sender, EventArgs e)
{
    using (ServerTextControl tx = new ServerTextControl())
    {
        // Initialize the new instance before using any other member
        if (!tx.Create())
        {
            MessageBox.Show("TX Text Control could not be initialized.");
            return;
        }

        tx.Text = "Thank you for your order.";
        tx.Save("confirmation.docx", StreamType.WordprocessingML);
    }
}
```

- [Create](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) initializes the resources of the instance and must be called first. It returns `false` if the instance couldn't be initialized.
- The `using` block disposes the instance at its end, which releases its resources. [Memory Usage and Resource Disposal](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/memory-usage/llms-full.txt) describes what happens to instances that are never disposed.

Create one instance for each piece of work, and don't keep instances in fields of the form for later use.

### Move Documents Between the Editor and ServerTextControl

Both classes load and save byte arrays, so a document moves between them without a file. [BinaryStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/binarystreamtype/enumeration/llms-full.txt) `InternalUnicodeFormat`, the TX Text Control format, keeps everything the document contains. The following handler builds a letter in a `ServerTextControl` and shows it in the editor:

```csharp
private void showLetterButton_Click(object sender, EventArgs e)
{
    byte[] document;

    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();

        tx.Selection.FontSize = 280; // 14 points, in twips
        tx.Selection.Text = "Order Confirmation\r\n";
        tx.Selection.FontSize = 220;
        tx.Selection.Text = "Thank you for your order.";

        tx.Save(out document, BinaryStreamType.InternalUnicodeFormat);
    }

    // Replace the document of the editor
    textControl1.Load(document, BinaryStreamType.InternalUnicodeFormat);
}
```

The other direction works the same way: `textControl1.Save(out byte[] data, BinaryStreamType.InternalUnicodeFormat)` hands the editor's document to a `ServerTextControl`, for example to convert or print it in the background. [TextControl.Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/load-method/llms-full.txt) replaces the document of the editor; [TextControl.Append](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/append-method/llms-full.txt) adds a document at its end instead. [Selection.FontSize](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/selection/fontsize-property/llms-full.txt) is set in twips, 20 per point.

### Work in the Background

`Load`, `Save` and the merge methods have no asynchronous versions: they occupy the thread that calls them until they return, so called in a button handler, they block the form. A `ServerTextControl` can run on another thread: run long work with `Task.Run`, give each thread its own instance, and keep the `TextControl`, which belongs to the UI thread, out of it. To process the editor's document in the background, save it to a byte array on the UI thread, as shown above, and pass the array to the task.

[UI Threading and Background Operations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/threading/llms-full.txt) shows these rules in code: it exports the editor's document in the background, brings a result back to the editor, converts a folder of files with progress and cancel, and explains why more threads don't make document processing proportionally faster. [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/printing/#print-without-blocking-the-form) prints in the background the same way.

### Merge Templates Without the Editor

A [MailMerge](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-documentserver/mailmerge/llms-full.txt) works with the text component it is connected through [TextComponent](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol-documentserver/mailmerge/textcomponent-property/llms-full.txt). Connected to a `ServerTextControl`, it creates documents while the editor shows something else; the result can then be saved, printed or loaded into the editor:

```csharp
private void mergeButton_Click(object sender, EventArgs e)
{
    string jsonData = File.ReadAllText("customers.json");
    byte[] result;

    using (ServerTextControl tx = new ServerTextControl())
    {
        tx.Create();

        TXTextControl.DocumentServer.MailMerge mailMerge = new TXTextControl.DocumentServer.MailMerge();
        mailMerge.TextComponent = tx;
        mailMerge.LoadTemplate("letter-template.docx", TXTextControl.DocumentServer.FileFormat.WordprocessingML);
        mailMerge.MergeJsonData(jsonData);

        tx.Save(out result, BinaryStreamType.InternalUnicodeFormat);
    }

    textControl1.Load(result, BinaryStreamType.InternalUnicodeFormat);
}
```

[Mail Merge and Reporting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/mail-merge/llms-full.txt) describes templates, data and merge settings.

### License ServerTextControl in a Class Library

`ServerTextControl` is covered by the license of TX Text Control .NET for Windows Forms and needs no server license. When it is used in a class library rather than in the executable of the application, the static property [ServerTextControl.EntryAssembly](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/servertextcontrol/entryassembly-property/llms-full.txt) names the assembly that holds the license. [Class Libraries and User Controls](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/installation/licensing/application/#class-libraries-and-user-controls) shows a class library that sets it and lists the versions that support it.

### Next Steps

- [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/creating/llms-full.txt): build documents in code.
- [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/saving-converting/llms-full.txt): the formats `Save` writes.
- [UI Threading and Background Operations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/threading/llms-full.txt): threads in a Windows Forms application.
- [Mail Merge and Reporting](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/mail-merge/llms-full.txt): create documents from templates and data.

---

## Nearby

- **Previous:** [Printing](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/printing/llms-full.txt)
- **Next:** [Exceptions and Error Handling](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/exceptions/llms-full.txt)
