TX Text Control .NET Server

Load a Document

Topic

Existing documents reach ServerTextControl by loading them. You load DOCX, RTF, HTML and PDF documents from a file, from a byte array and from a string, pick the right format enumeration for each case and use LoadSettings to import the fields of a Word template.

Prerequisites

  • A console application that references TXTextControl.TextControl.Core.SDK, as created in Create Your First Application.
  • One or more sample documents. In Visual Studio, add a folder named Documents to the project, add your files to it with Add Existing Item..., and set the Copy to Output Directory property of each file to Copy always. The examples below then find the files at Documents/... when the application runs.

How Loading Works

You load a document with the ServerTextControl.Load method. Keep these points in mind:

  • Load replaces the complete contents of the ServerTextControl. To add a document to the end of existing content instead, use ServerTextControl.Append.
  • You always pass the format of the data. Which enumeration you use depends on where the data comes from:
SourceFirst parameterFormat parameter
File pathstringStreamType
File streamSystem.IO.FileStreamStreamType
Byte arraybyte[]BinaryStreamType
StringstringStringStreamType

Every overload also accepts a LoadSettings object as an optional third parameter.

Common Formats

The examples on this page use WordprocessingML for DOCX, RichTextFormat for RTF, HTMLFormat for HTML, AdobePDF for PDF and PlainText for plain text. The same name is used in each enumeration that has a value for the format. Formats and StreamType Values lists all formats with their values, and which of them can only be saved. DOC requires the Classic package TXTextControl.TextControl.ASP.SDK on Windows.

Load From a File

Pass the path of the file and a StreamType value. The following example loads a DOCX file and prints the number of pages:

using TXTextControl;

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

    // Load a DOCX file
    tx.Load("Documents/sample.docx", StreamType.WordprocessingML);

    Console.WriteLine($"Pages: {tx.Pages}");
}

Other formats work the same way. Only the path and the StreamType value change:

tx.Load("Documents/sample.rtf", StreamType.RichTextFormat);
tx.Load("Documents/sample.html", StreamType.HTMLFormat);
tx.Load("Documents/sample.pdf", StreamType.AdobePDF);

Load From a Byte Array

A byte array is the usual choice when the document does not come from the local file system, for example from a database, from cloud storage or from an HTTP request. Pass the bytes and a BinaryStreamType value:

using TXTextControl;

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

    byte[] document = File.ReadAllBytes("Documents/sample.docx");

    // Load the document from the byte array
    tx.Load(document, BinaryStreamType.WordprocessingML);

    // Save the document as PDF
    tx.Save("result.pdf", StreamType.AdobePDF);
}

Load has an overload for a System.IO.FileStream, which takes a StreamType value and reads from the current position of the stream:

using (FileStream fileStream = File.OpenRead("Documents/sample.docx"))
{
    tx.Load(fileStream, StreamType.WordprocessingML);
}

There is no overload for other stream types, such as a MemoryStream or the stream of an uploaded file. Copy such a stream into a byte array first and load the byte array:

static byte[] ToByteArray(Stream stream)
{
    using (MemoryStream memoryStream = new MemoryStream())
    {
        stream.CopyTo(memoryStream);
        return memoryStream.ToArray();
    }
}

Load From a String

Text-based formats such as HTML, RTF and plain text can be loaded directly from a string with a StringStreamType value. The following example loads an HTML string and saves it as PDF:

using TXTextControl;

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

    string html = "<strong>This is a <span style=\"color: red;\">sample</span> <u>document</u>!</strong>";

    // Load the HTML string
    tx.Load(html, StringStreamType.HTMLFormat);

    tx.Save("result.pdf", StreamType.AdobePDF);
}

To load plain text, use StringStreamType.PlainText. The end of a paragraph is marked with a carriage return and a line feed (\r\n):

tx.Load("First paragraph\r\nSecond paragraph", StringStreamType.PlainText);

Load a PDF

A PDF is loaded like the other formats, with StreamType.AdobePDF or BinaryStreamType.AdobePDF. Because PDF is a page description format, TX Text Control rebuilds an editable document from the text it can extract, and LoadSettings.PDFImportSettings controls how. Load and Modify a PDF explains the import modes and their limits and shows how to read the text of a PDF.

Load Word Templates With Fields

Word documents often contain fields, for example the merge fields of a mail merge template. By default, fields that are defined by Microsoft Word are not imported as fields, and the ServerTextControl.ApplicationFields collection stays empty. To keep them, set LoadSettings.ApplicationFieldFormat before you load the document:

using TXTextControl;

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

    LoadSettings loadSettings = new LoadSettings()
    {
        // Import Microsoft Word fields such as MERGEFIELD
        ApplicationFieldFormat = ApplicationFieldFormat.MSWord
    };

    tx.Load("Documents/template.docx", StreamType.WordprocessingML, loadSettings);

    Console.WriteLine($"Fields: {tx.ApplicationFields.Count}");
}

The ApplicationFieldFormat values you use most often when loading:

ValueResult
NoneDefault. Fields of other applications are not imported as application fields. Form fields are imported as TX Text Control form fields.
MSWordFields defined by Microsoft Word are imported as application fields.
MSWordTXFormFieldsLike MSWord, but form fields such as check boxes, combo boxes and form text fields are imported as TX Text Control form fields.

This setting applies to Microsoft Word and RTF documents only. Loading a Word template with its merge fields is the first step of a mail merge, which Simple Mail Merge walks you through.

Next Steps