TX Text Control .NET for WPF

ServerTextControl

Topic

A WPF application often has documents to build, merge or convert that the user never needs to watch being made. ServerTextControl is the document engine of TX Text Control without an element on screen: it runs the same programming interface as the editor in memory, and when a result should be seen, the WPF.TextControl in your window loads it. Read on for when a WPF application uses it, the pattern every use follows and how documents travel between the two classes.

What ServerTextControl Is

ServerTextControl is the class TXTextControl.ServerTextControl in the assembly TXTextControl.Server.dll, which the NuGet package TXTextControl.TextControl.WPF.SDK includes. It is derived from System.ComponentModel.Component, not from a WPF control, so it has no template, no visual tree and no place in XAML. You create it in code whenever you need it.

It shares the document interface of WPF.TextControl: the Load, Save and Append methods, the Selection, and the collections of tables, images, fields, sections and other elements. The classes of these elements are in the TXTextControl namespace and are the same for both, so a method that builds a document can take either object.

The client license of TX Text Control .NET for WPF covers ServerTextControl and MailMerge in desktop applications. Text Control opened both classes to the Windows Forms and WPF products with version X8, without a separate server or runtime license.

When a WPF Application Uses It

  • Preparing a document before it is shown. Code assembles or merges a document in a ServerTextControl and hands the finished result to the editor, so the user sees no intermediate steps.
  • Work that has nothing to do with the open document. Converting files to PDF, creating a series of letters or exporting a report runs apart from the document in the editor.
  • Mail merge in the background. A MailMerge whose TextComponent is a ServerTextControl creates documents while the editor shows something else.

Without a user interface, a ServerTextControl keeps no undo history and renders nothing for the screen. Text Control names better performance, support for several threads and a smaller memory footprint as the advantages over a visible control. Performance covers background work and the WPF dispatcher.

How You Work With It

Each use follows the same three steps:

  1. Create and initialize. Create an instance and call Create before any other member. Put the instance into a using statement, which disposes it and releases its resources when the work is done.
  2. Load or build the document. Load replaces the contents with a document, Append adds one at the end, and Selection inserts and formats text.
  3. Save the result. Save writes the whole document to a file, a stream, a byte array or a string.

The format is a StreamType for files and streams, a BinaryStreamType for byte arrays and a StringStreamType for strings. Supported File Formats lists the values.

Hand a Document to the Editor

WPF.TextControl has the same byte array overloads of Load and Save, so a document built in a ServerTextControl moves into the editor as a byte array in the TX Text Control format, without a conversion. Show a Document Built in Code shows the code, and Create a Document Programmatically builds a first document step by step.

Key Members

MemberPurpose
CreateInitializes the resources of a new instance. It must be called first.
Load, AppendReplace the contents with a document, or add one at the end.
SaveSaves the complete document in the format you pass.
Text, SelectionThe text of the document, and the selection that inserts and formats text.
Tables, Images, HeadersAndFootersThe tables, images, headers and footers.
ApplicationFields, FormFieldsFields such as MS Word merge fields, and form fields.
Comments, TrackedChangesThe comments and the tracked changes.
PrintPrints without showing the document, using a System.Drawing.Printing.PrintDocument.
EntryAssemblyA static property that names the assembly holding the license, for example when the class is used in a class library.

The ServerTextControl reference lists all members.

Next Steps