# Using ServerTextControl in Desktop Applications

> Keep a WPF window responsive with ServerTextControl: create it in code instead of XAML, hand documents to the TextControl as byte arrays and merge templates.

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

---

Not every document in a WPF application needs a window. `ServerTextControl` is TX Text Control's document engine without a user interface: it creates, loads, merges, converts and prints documents with the same members as the WPF `TextControl`, but no XAML, no rendering and no undo history. Keep the `TextControl` for the document the user edits, and give everything else to a `ServerTextControl`, on a background thread when the work takes time, so that the window never freezes.

The component is introduced in [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/overview/components/servertextcontrol/llms-full.txt) in the Overview. The code-behind examples use the `TextControl` named `textControl1` in *MainWindow.xaml*, and *MainWindow.xaml.cs* needs these `using` directives:

```csharp
using System.Windows;
using TXTextControl;
```

### Which Class Does What

The WPF `TextControl` shows the document and belongs to the UI thread of the window. [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/llms-full.txt) has no user interface, can be used on any thread and shares the document members of the editor, such as `Load`, `Save`, `Selection` and `Tables`, so code that builds a document works with either. [What ServerTextControl Is](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/overview/components/servertextcontrol/#what-servertextcontrol-is) in the Overview describes the class and its key members.

### Create and Dispose Each Instance

A `ServerTextControl` isn't declared in XAML. Your code creates it, initializes it with [Create](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) before anything else, and disposes it when the work is done:

```csharp
private void CreateDocument_Click(object sender, RoutedEventArgs e)
{
    using (ServerTextControl tx = new ServerTextControl())
    {
        // false if the instance couldn't be initialized
        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);
    }
}
```

The `using` block disposes the instance and releases its resources, even when an exception occurs. Create an instance for each task rather than keeping one in a field of the window. [Memory Usage and Resource Disposal](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/performance/memory-usage/llms-full.txt) explains why disposal matters.

### Show a Document Built in Code

The WPF `TextControl` and `ServerTextControl` both load and save byte arrays. With [BinaryStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/binarystreamtype/enumeration/llms-full.txt) `InternalUnicodeFormat`, the TX Text Control format, a document moves between them completely:

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

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

        // Font sizes in twips: 280 = 14 points
        tx.Selection.FontSize = 280;
        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);
    }

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

[WPF.TextControl.Load](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/load-method/llms-full.txt) replaces the document in the editor, and [WPF.TextControl.Append](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/append-method/llms-full.txt) adds one at its end. In the other direction, `textControl1.Save(out byte[] data, BinaryStreamType.InternalUnicodeFormat)` gives the editor's document to a `ServerTextControl`. [Selection.FontSize](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/fontsize-property/llms-full.txt) is measured in twips, 20 per point.

### Keep the Window Responsive

`Load`, `Save` and the merge methods run synchronously and block the window while they run on the UI thread. Long work therefore goes to a thread of the thread pool with `Task.Run`, following three rules: the WPF `TextControl` stays on the UI thread and hands over its document as a byte array, every thread creates, uses and disposes its own `ServerTextControl`, and more threads don't make document processing proportionally faster. The Performance section explains each rule with code:

- [Hand a Document to Another Thread](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/performance/dispatcher/#hand-a-document-to-another-thread) exports the editor's document as a PDF in the background.
- [Convert Many Files With Progress and Cancel](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/performance/background-operations/#convert-many-files-with-progress-and-cancel) converts a folder with a progress bar and a **Cancel** button.
- [How Far Threads Take You](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/performance/background-operations/#how-far-threads-take-you) explains the shared resources and when several processes are the better choice.

[Printing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/printing/#print-a-file-in-the-background) prints in the background the same way.

### Merge Templates Without the Editor

A [MailMerge](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver/mailmerge/llms-full.txt) connected to a `ServerTextControl` through [TextComponent](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-documentserver/mailmerge/textcomponent-property/llms-full.txt) creates documents from templates and data without changing the editor, and the finished document goes to the editor as a byte array, as shown above. [Merge the Data](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/mail-merge/fields/#merge-the-data) in Getting Started shows the code. Templates, data sources and merge settings are the subject of [Mail Merge and Reporting](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/llms-full.txt).

### License ServerTextControl in a Class Library

`ServerTextControl` is part of TX Text Control .NET for WPF and needs no server license. If it runs in a class library instead of the application's executable, set the static property [ServerTextControl.EntryAssembly](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/entryassembly-property/llms-full.txt) to the assembly that holds the license before the first instance is created, as [Application Licensing](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/installation/licensing/application/llms-full.txt) shows.

### Next Steps

- [Creating Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/creating/llms-full.txt): build documents in code.
- [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/saving-converting/llms-full.txt): the formats `Save` writes.
- [Background Operations](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/performance/background-operations/llms-full.txt): long-running work in WPF applications.
- [Mail Merge and Reporting](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/mail-merge/llms-full.txt): create documents from templates and data.

---

## Nearby

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