# Save as DOCX and PDF

> Export the document of the WPF TextControl as DOCX and PDF with Save, and convert DOC files to DOCX and PDF with ServerTextControl and an OpenFileDialog.

- **Product:** TX Text Control .NET for WPF
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control .NET for WPF > Getting Started > Document Processing > Save as DOCX and PDF
- **1936 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/save-docx-pdf/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/save-docx-pdf/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/save-docx-pdf/llms-full.txt

---

TX Text Control converts a document by loading it in one format and saving it in another, so DOCX and PDF output is a matter of the format you pass to `Save`. In a WPF window, the document to convert is either the one the user edits in the `TextControl` or a file your code opens in the background. The two button handlers below cover both: exporting the editor's document and converting a Word 97-2003 file.

### Prerequisites

- The application from [Create Your First WPF Application](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/first-application/llms-full.txt), with a `TextControl` named `textControl1` in *MainWindow.xaml*.
- The Professional or Enterprise edition, which includes DOC, DOCX and PDF. See [Editions and Feature Availability](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/overview/editions/llms-full.txt).

Each section adds a button to the `DockPanel` of *MainWindow.xaml*, before the `TextControl` element, with a `Click` handler in *MainWindow.xaml.cs*. For example:

```xml
<Button x:Name="exportButton" DockPanel.Dock="Top" Content="Export"
        Click="exportButton_Click" />
```

*MainWindow.xaml.cs* needs these `using` directives:

```csharp
using System;
using System.IO;
using System.Windows;
using Microsoft.Win32;
using TXTextControl;
```

### Export the Editor's Document

[Save](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol-wpf/textcontrol/save-method/llms-full.txt) of the `TextControl` writes the complete document in the format that its [StreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/streamtype/enumeration/llms-full.txt) argument names. Two calls produce a DOCX and a PDF file in the user's *Documents* folder:

```csharp
private void exportButton_Click(object sender, RoutedEventArgs e)
{
    string folder = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

    // The complete document, once as DOCX and once as PDF
    textControl1.Save(System.IO.Path.Combine(folder, "document.docx"), StreamType.WordprocessingML);
    textControl1.Save(System.IO.Path.Combine(folder, "document.pdf"), StreamType.AdobePDF);

    MessageBox.Show(this, "document.docx and document.pdf are in " + folder);
}
```

`StreamType.WordprocessingML` stands for DOCX, the Office Open XML format of Microsoft Word, and `StreamType.AdobePDF` for PDF. Files that exist already are overwritten. The PDF tutorial [Create a PDF](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/pdf/create/llms-full.txt) adds document properties and passwords and lets the user choose the file name.

### Convert a File Without the Editor

A [ServerTextControl](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/llms-full.txt) converts files without showing them and without changing the document in the editor. The following handler asks for a Word 97-2003 file with the `OpenFileDialog` of WPF and saves it as DOCX and as PDF in the same folder:

```csharp
private void convertButton_Click(object sender, RoutedEventArgs e)
{
    OpenFileDialog dialog = new OpenFileDialog()
    {
        Filter = "Word 97-2003 documents (*.doc)|*.doc"
    };

    if (dialog.ShowDialog(this) != true)
    {
        return;
    }

    using (ServerTextControl tx = new ServerTextControl())
    {
        // Initialize the new instance before using it
        tx.Create();

        tx.Load(dialog.FileName, StreamType.MSWord);

        // Write both files into the folder of the source file
        tx.Save(System.IO.Path.ChangeExtension(dialog.FileName, ".docx"), StreamType.WordprocessingML);
        tx.Save(System.IO.Path.ChangeExtension(dialog.FileName, ".pdf"), StreamType.AdobePDF);
    }
}
```

The code works as follows:

- `Microsoft.Win32.OpenFileDialog` is the file dialog of WPF. Its `ShowDialog` returns a nullable Boolean, which is `true` when the user has chosen a file.
- [Create](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/create-method/llms-full.txt) initializes the `ServerTextControl` before [Load](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/load-method/llms-full.txt) reads the file. `StreamType.MSWord` is the binary Word format, DOC.
- [ServerTextControl.Save](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/servertextcontrol/save-method/llms-full.txt) writes the document twice, and the `using` statement disposes the instance afterward.

Any format that TX Text Control loads can be converted this way into any format it saves; [Supported File Formats](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/overview/file-formats/llms-full.txt) has the list.

### Save to Byte Arrays

Documents that go into a database, a web service call or an email attachment need no file. With a [BinaryStreamType](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/reference/txtextcontrol/binarystreamtype/enumeration/llms-full.txt) such as `WordprocessingML` or `AdobePDF` instead of a file name, `Save` returns the document in a byte array, and `Load` reads one; [Load and Save Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/load-save/#load-and-save-without-a-dialog-box) shows both calls.

### Create a PDF/A Copy in the Background

PDF/A, the PDF format for long-term archiving, needs font settings that you may not want to change in the editor. [Create a PDF/A Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/pdf/create/#create-a-pdf-a-document) shows how to save the PDF/A file from a copy of the editor's document in a `ServerTextControl`.

### Next Steps

- [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms-full.txt): print instead of saving.
- [Create a PDF](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/pdf/create/llms-full.txt): document properties, the Save As dialog, passwords and permissions.
- [Saving and Converting Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/document-processing/saving-converting/llms-full.txt): every save and conversion option in detail.
- [Load and Save Documents](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-editor/load-save/llms-full.txt): open and save files from the editor with the built-in dialogs.

---

## Nearby

- **Previous:** [Create a Document Programmatically](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/create/llms-full.txt)
- **Next:** [Print a Document](https://docs.textcontrol.com/tx-text-control-dotnet-wpf/getting-started/document-processing/print/llms-full.txt)
