# Loading and Saving Documents

> Open and save documents in the WPF TextControl with the built-in dialog boxes: WPF.DialogResult, file filters, DialogSettings, byte arrays and HTML strings.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Document Editor > Loading and Saving Documents
- **3213 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/load-save/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/load-save/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/load-save/llms-full.txt

---

Where does a document come from, and where does it go? In a WPF editor, the answer is rarely just a file the user picks: documents are read from databases, exchanged as HTML strings, inserted at the cursor or appended as an appendix, and some files fail to load. `TextControl` handles all of this through its `Load` and `Save` methods and the settings objects they accept.

[Load and Save Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/load-save/llms-full.txt) in Getting Started builds the basic **New**, **Open**, **Save** and **Save As** commands. The examples here go further. They assume a `TextControl` named `textControl1` in *MainWindow.xaml*, and the code goes into *MainWindow.xaml.cs* with `using TXTextControl;` and, where noted, `using System.IO;`.

### How Loading and Saving Works

[Load](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/load-method/llms-full.txt) replaces everything in the editor with the loaded document, and [Save](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/save-method/llms-full.txt) writes the complete document. Which overload you call depends on where the document comes from or goes to, and each kind uses its own enumeration of formats:

| Source or target | Format enumeration | Example |
|---|---|---|
| A dialog box | [StreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/streamtype/enumeration/llms-full.txt), as file filters | `Load(StreamType.WordprocessingML)` |
| A file path | `StreamType` | `Load("report.docx", StreamType.WordprocessingML)` |
| A `FileStream` | `StreamType` | `Load(stream, StreamType.RichTextFormat)` |
| A byte array | [BinaryStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/binarystreamtype/enumeration/llms-full.txt) | `Load(data, BinaryStreamType.InternalUnicodeFormat)` |
| A string | [StringStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/stringstreamtype/enumeration/llms-full.txt) | `Load(html, StringStreamType.HTMLFormat)` |

A [LoadSettings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsettings/llms-full.txt) or [SaveSettings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/savesettings/llms-full.txt) object can be passed to every overload. It carries options into the operation and brings back information, such as the name of the file the user selected.

[Formats and StreamType Values](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/overview/file-formats/#formats-and-streamtype-values) in Supported File Formats lists which format each of the three enumerations offers, and which formats can only be loaded or only be saved.

A loaded PDF document contributes its text, and [PDFImportSettings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsettings/pdfimportsettings-property/llms-full.txt) decides how paragraphs or text frames are generated from it; see [PDF Import and Its Limitations](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/pdf/import/llms-full.txt).

### Open and Save With the Built-In Dialog Boxes

The following XAML adds an **Open** and a **Save As** button above the editor:

```xml
<DockPanel>
    <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
        <Button Content="Open..." Click="openButton_Click" />
        <Button Content="Save As..." Click="saveAsButton_Click" />
    </StackPanel>
    <WPF:TextControl x:Name="textControl1" />
</DockPanel>
```

Called without a file name, `Load` and `Save` open the **Open** and **Save As** dialog boxes; `Load()` without any parameter offers all supported formats. In WPF, both return a [TXTextControl.WPF.DialogResult](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/dialogresult/enumeration/llms-full.txt) value, not the `DialogResult` of Windows Forms: `OK` when the user confirmed the dialog box, `Cancel` when it was canceled and `None` when it was left with the Escape key.

```csharp
private void openButton_Click(object sender, RoutedEventArgs e)
{
    LoadSettings loadSettings = new LoadSettings()
    {
        // Preselect DOCX in the file type list
        DefaultStreamType = StreamType.WordprocessingML
    };

    TXTextControl.WPF.DialogResult result = textControl1.Load(
        StreamType.WordprocessingML | StreamType.MSWord | StreamType.RichTextFormat |
        StreamType.HTMLFormat | StreamType.InternalUnicodeFormat | StreamType.AllFormats,
        loadSettings);

    if (result == TXTextControl.WPF.DialogResult.OK)
    {
        Title = loadSettings.LoadedFile;
    }
}

private void saveAsButton_Click(object sender, RoutedEventArgs e)
{
    SaveSettings saveSettings = new SaveSettings();

    TXTextControl.WPF.DialogResult result = textControl1.Save(
        StreamType.WordprocessingML | StreamType.HTMLFormat | StreamType.AdobePDF,
        saveSettings,
        SaveSettings.DialogSettings.EnterPassword |
        SaveSettings.DialogSettings.StylesheetOptions |
        SaveSettings.DialogSettings.SaveSelection);

    if (result == TXTextControl.WPF.DialogResult.OK)
    {
        Title = saveSettings.SavedFile;
    }
}
```

The code works as follows:

- **File filters.** The combined `StreamType` values become the file type filters. `StreamType.AllFormats` adds an **All Formats** entry to the **Open** dialog box, and [DefaultStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsavesettingsbase/defaultstreamtype-property/llms-full.txt) preselects a filter.
- **The chosen file.** After **OK**, [LoadedFile](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsettings/loadedfile-property/llms-full.txt) and [LoadedStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsettings/loadedstreamtype-property/llms-full.txt), or [SavedFile](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/savesettings/savedfile-property/llms-full.txt) and [SavedStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/savesettings/savedstreamtype-property/llms-full.txt), contain the path and format. Keep them for a later **Save** without a dialog box.
- **Dialog options.** [SaveSettings.DialogSettings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/savesettings/dialogsettings-enumeration/llms-full.txt), introduced in version 31.0, adds options that appear only when they apply: `EnterPassword` a button for an encryption password when PDF is chosen (PDF/A has no encryption passwords), `StylesheetOptions` a choice between inline styles and a separate CSS file when HTML is chosen, and `SaveSelection` a check box that saves only the selected text when text is selected.

### Control What Is Loaded

Settings on the `LoadSettings` object change what is imported, for example [LoadImages](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsettings/loadimages-property/llms-full.txt) to leave images out or [ApplicationFieldFormat](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/loadsettings/applicationfieldformat-property/llms-full.txt) to import the merge fields of Word documents. After loading, the same object tells you which file was loaded, its format and its metadata, such as its title and author. [Control the Import With LoadSettings](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/loading/#control-the-import-with-loadsettings) lists every setting with its default, and [Read Information About a Loaded Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/loading/#read-information-about-a-loaded-document) what comes back. [Document Properties](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/properties/llms-full.txt) edits the properties in the editor.

### Keep Documents in a Database

A byte array in the TX Text Control format holds everything the editor supports. Use it for documents that are stored and edited again:

```csharp
// The complete document in the TX Text Control format
byte[] data;
textControl1.Save(out data, BinaryStreamType.InternalUnicodeFormat);

// Store data in the database, and load it again later
textControl1.Load(data, BinaryStreamType.InternalUnicodeFormat);
```

`InternalUnicodeFormat` saves text as Unicode and `InternalFormat` as ANSI. According to the blog article [Document Lifecycle Optimization: Leveraging TX Text Control's Internal Format](https://www.textcontrol.com/blog/2025/05/16/document-lifecycle-optimization-leveraging-tx-text-controls-internal-format/), documents are best kept in this format while they are worked on, and converted to DOCX or PDF only when they are distributed.

HTML, RTF, XML and plain text can also be exchanged as strings:

```csharp
string html;
textControl1.Save(out html, StringStreamType.HTMLFormat);

textControl1.Load("<p>Hello <b>World</b></p>", StringStreamType.HTMLFormat);
```

The stream overloads accept a `FileStream` and read from its current position. Documents in a `MemoryStream` or another stream go through the byte array overloads.

### Save HTML With a Separate Stylesheet

[CssSaveMode](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/savesettings/csssavemode-property/llms-full.txt) of the `SaveSettings` decides whether the formatting of an HTML document is written into its tags, into its head or into a separate CSS file. [Save HTML With Stylesheets and Images](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/saving-converting/#save-html-with-stylesheets-and-images) describes each value and embeds the images. In the **Save As** dialog box, `SaveSettings.DialogSettings.StylesheetOptions` lets the user make this choice, as shown above.

### Insert, Append and Save Parts of a Document

[Load](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/load-method/llms-full.txt) of the [Selection](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/selection-property/llms-full.txt) inserts a document at the input position or in place of the selected text, and [Append](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/append-method/llms-full.txt) adds one at the end; [Load, Append or Insert](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/loading/#load-append-or-insert) compares them with `Load` and shows the code. [Save](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/selection/save-method/llms-full.txt) of the `Selection` saves only the selected text, as [Save Part of a Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/saving-converting/#save-part-of-a-document) shows; in the **Save As** dialog box, `SaveSettings.DialogSettings.SaveSelection` offers the same to the user.

### Handle Errors

When a document can't be converted, `Load` throws a [FilterException](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/filterexception/llms-full.txt) whose [Reason](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/filterexception/reason-property/llms-full.txt) names the cause, such as `WrongFileType` or `InvalidPassword`. [Catch Errors When Opening a Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/exceptions/#catch-errors-when-opening-a-document) shows the `try` block. `InvalidPassword` means that a PDF document needs its user password; [Load Password-Protected PDF Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/loading/#load-password-protected-pdf-documents) loads it again with the password, and the TX Words sample catches the exception the same way and opens a password dialog box of its own.

### Next Steps

- [Document Properties](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/properties/llms-full.txt): title, author and user-defined properties of a document.
- [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/saving-converting/llms-full.txt): conversions without an editor.
- [Handling Untrusted Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/security/untrusted-documents/llms-full.txt): precautions for files from unknown sources.
- [Architecture and Control Lifecycle](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/architecture/llms-full.txt): when the control is ready to load a document.

---

## Nearby

- **Previous:** [Architecture and Control Lifecycle](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/architecture/llms-full.txt)
- **Next:** [Document Properties](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-editor/properties/llms-full.txt)
