TX Text Control .NET for WPF

Save as DOCX and PDF

Topic

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

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:

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

MainWindow.xaml.cs needs these using directives:

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

Export the Editor's Document

Save of the TextControl writes the complete document in the format that its StreamType argument names. Two calls produce a DOCX and a PDF file in the user's Documents folder:

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 adds document properties and passwords and lets the user choose the file name.

Convert a File Without the Editor

A ServerTextControl 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:

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 initializes the ServerTextControl before Load reads the file. StreamType.MSWord is the binary Word format, DOC.
  • ServerTextControl.Save 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 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 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 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 shows how to save the PDF/A file from a copy of the editor's document in a ServerTextControl.

Next Steps