# Loading and Saving Documents

> Configure the Open and Save As dialog boxes of a Windows Forms TextControl: file filters, DefaultStreamType, DialogSettings options and the file the user chose.

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

---

Once the **Open** and **Save As** buttons of a first editor work, real applications need more: the right file filters, a preselected format, a password or stylesheet option in the **Save As** dialog box, documents from a database and a clear message when a file can't be read. The `Load` and `Save` methods of `TextControl` and their settings objects cover all of these. The sections below configure the dialog boxes of the editor and point to the pages that describe each of the other options.

If you haven't added the basic commands yet, start with [Load and Save Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/getting-started/document-editor/load-save/llms-full.txt) in Getting Started: it builds **New**, **Open**, **Save** and **Save As** with a save-changes prompt. The examples below extend that form and use a `TextControl` named `textControl1` on `Form1`. They need `using TXTextControl;`.

### How Loading and Saving Works

[Load](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/load-method/llms-full.txt) replaces the complete contents of the editor, and [Save](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/textcontrol/save-method/llms-full.txt) writes the complete document. Both methods come with the same sources and targets, and each kind has its own format enumeration:

| Source or target | Format enumeration | Example |
|---|---|---|
| A dialog box | [StreamType](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/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-winforms/reference/txtextcontrol/binarystreamtype/enumeration/llms-full.txt) | `Load(data, BinaryStreamType.InternalUnicodeFormat)` |
| A string | [StringStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/stringstreamtype/enumeration/llms-full.txt) | `Load(html, StringStreamType.HTMLFormat)` |

Each of these sources and targets also has an overload that takes a [LoadSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsettings/llms-full.txt) or [SaveSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/llms-full.txt) object. It passes options into the operation and returns information about it, such as the file the user chose.

[Supported File Formats](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/overview/file-formats/#formats-and-streamtype-values) lists which formats each enumeration contains and which of them are save only. Loading PDF imports its text; how the structure of the document is rebuilt depends on [PDFImportSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsettings/pdfimportsettings-property/llms-full.txt), as [PDF Import and Its Limitations](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/pdf/import/llms-full.txt) explains.

### Configure the Open Dialog Box

Without a file name, `Load` opens the **Open** dialog box. The `StreamType` values you combine become its file type filters, `StreamType.AllFormats` adds an **All Formats** entry, and [DefaultStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsavesettingsbase/defaultstreamtype-property/llms-full.txt) preselects one of them:

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

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

    if (result == DialogResult.OK)
    {
        Text = loadSettings.LoadedFile;
    }
}
```

The method returns the `DialogResult` of the dialog box. After **OK**, [LoadedFile](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsettings/loadedfile-property/llms-full.txt) and [LoadedStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/loadsettings/loadedstreamtype-property/llms-full.txt) contain the path and format of the opened file, which your **Save** command needs later.

### Offer Options in the Save As Dialog Box

`Save` without a file name opens the **Save As** dialog box. The overload with a [SaveSettings.DialogSettings](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/dialogsettings-enumeration/llms-full.txt) value adds options to it:

```csharp
private void saveAsButton_Click(object sender, EventArgs e)
{
    SaveSettings saveSettings = new SaveSettings();

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

    if (result == DialogResult.OK)
    {
        Text = saveSettings.SavedFile;
    }
}
```

Each option appears only when it applies:

- **`EnterPassword`** adds a button that opens a dialog box for an encryption password, when the user chooses Adobe PDF. PDF/A doesn't support encryption passwords.
- **`StylesheetOptions`** lets the user choose how formatting styles are saved, inline or in a separate CSS file, when the user chooses HTML.
- **`SaveSelection`** adds a check box to save only the selected text, when text is selected.

The following screenshot shows the stylesheet options of the TX Words sample application for an HTML file:

![The Save As dialog box of TX Words with HTML selected as the file type and the stylesheet options None, Inline and as .css File, with a Browse button for the CSS file.](https://docs.textcontrol.com/assets/img/tx-text-control-dotnet-winforms/save-as-stylesheet-options.295f662b.webp)

[SavedFile](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/savedfile-property/llms-full.txt) and [SavedStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/savedstreamtype-property/llms-full.txt) tell you what the user chose, and [BytesWritten](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/reference/txtextcontrol/savesettings/byteswritten-property/llms-full.txt) the size of the saved file. The `DialogSettings` values were introduced in version 31.0.

### Load and Save Without a Dialog Box

The other overloads load and save without asking the user, and the settings objects change what is imported and written. `Load` and `Save` of the `TextControl` are the same methods as those of `ServerTextControl`, so the Document Processing pages describe them for both:

| If you want to | Read |
|---|---|
| Import Word merge fields, skip images or hypertext links, or read the title and file of a loaded document | [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/#control-the-import-with-loadsettings) |
| Insert a document at the input position with `Selection.Load`, or add one at the end with `Append` | [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/#load-append-or-insert) |
| Load byte arrays, strings and streams | [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/#load-from-byte-arrays-strings-and-streams) |
| Save only the selected text or a paragraph | [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/saving-converting/#save-part-of-a-document) |
| Save HTML with a separate CSS file | [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/saving-converting/#save-html-with-stylesheets-and-images) |
| Handle a `FilterException`, such as a file of the wrong type or a PDF that needs a password | [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/#handle-errors) |

The title, author and other properties of a loaded document are covered in [Document Properties](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/properties/llms-full.txt).

### Store Documents in a Database

A document in a database is a byte array: `textControl1.Save(out data, BinaryStreamType.InternalUnicodeFormat)` writes it, and `textControl1.Load(data, BinaryStreamType.InternalUnicodeFormat)` reads it back. The TX Text Control format keeps everything the editor supports, in Unicode with `InternalUnicodeFormat` and in ANSI with `InternalFormat`, which is why Text Control recommends it for documents that are still edited, as [Document Loading and Conversion](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/performance/loading-conversion/#keep-the-tx-text-control-format-while-a-document-changes) explains. [Data Binding and Database Storage](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/integration/data-binding/llms-full.txt) connects the editor to a record and writes the bytes to a database column.

### Next Steps

- [Document Properties](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/properties/llms-full.txt): title, author and user-defined properties that travel with the document.
- [Loading Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/loading/llms-full.txt): every option of `LoadSettings` and the errors a load can raise.
- [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-processing/saving-converting/llms-full.txt): convert documents without an editor.
- [Handling Untrusted Documents](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/security/untrusted-documents/llms-full.txt): what to consider before loading files from unknown sources.
- [Printing and Print Preview](https://docs.textcontrol.com/tx-text-control-dotnet-winforms/document-editor/printing/llms-full.txt): print the document instead of saving it.

---

## Nearby

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