# Load a Document

> Load DOCX, RTF, HTML, PDF and other formats into ServerTextControl from a file, a byte array or a string, and use LoadSettings for Word templates.

- **Product:** TX Text Control .NET Server
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET Server > Getting Started > Document Processing > Load a Document
- **2603 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/load/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/load/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/load/llms-full.txt

---

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](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/first-application/llms-full.txt).
- 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](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/load-method/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/append-method/llms-full.txt).
- You always pass the format of the data. Which enumeration you use depends on where the data comes from:

| Source | First parameter | Format parameter |
|---|---|---|
| File path | `string` | [StreamType](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/streamtype/enumeration/llms-full.txt) |
| File stream | `System.IO.FileStream` | [StreamType](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/streamtype/enumeration/llms-full.txt) |
| Byte array | `byte[]` | [BinaryStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/binarystreamtype/enumeration/llms-full.txt) |
| String | `string` | [StringStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/stringstreamtype/enumeration/llms-full.txt) |

Every overload also accepts a [LoadSettings](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/loadsettings/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-dotnet-server/overview/file-formats/#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:

```csharp
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:

```csharp
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:

```csharp
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:

```csharp
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:

```csharp
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:

```csharp
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`):

```csharp
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](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/loadsettings/pdfimportsettings-property/llms-full.txt) controls how. [Load and Modify a PDF](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/pdf/load-modify/#what-happens-when-you-load-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](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/servertextcontrol/applicationfields-property/llms-full.txt) collection stays empty. To keep them, set [LoadSettings.ApplicationFieldFormat](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/loadsettings/applicationfieldformat-property/llms-full.txt) before you load the document:

```csharp
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](https://docs.textcontrol.com/tx-text-control-dotnet-server/reference/txtextcontrol/applicationfieldformat/enumeration/llms-full.txt) values you use most often when loading:

| Value | Result |
|---|---|
| `None` | Default. Fields of other applications are not imported as application fields. Form fields are imported as TX Text Control form fields. |
| `MSWord` | Fields defined by Microsoft Word are imported as application fields. |
| `MSWordTXFormFields` | Like `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](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/mail-merge/simple/llms-full.txt) walks you through.

### Next Steps

- [Create a Document](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/create/llms-full.txt) — add formatted text, tables and headers in code.
- [Save as DOCX and PDF](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/save-docx-pdf/llms-full.txt) — convert the loaded document to another format.
- [Simple Mail Merge](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/mail-merge/simple/llms-full.txt) — merge data into a loaded template.
- [Load and Modify a PDF](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/pdf/load-modify/llms-full.txt) — work with existing PDF files.

---

## Nearby

- **Previous:** [Create Your First Application](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/first-application/llms-full.txt)
- **Next:** [Create a Document](https://docs.textcontrol.com/tx-text-control-dotnet-server/getting-started/document-processing/create/llms-full.txt)
