Skip to main content

Import and Export Word Processing Documents

  • 7 minutes to read

This document describes how to load and save documents in different formats.

Supported Formats

The Word Processing Document API supports the following document formats:

  • TXT

    Plain Text format.

  • RTF

    Rich Text Format.

  • DOCX

    Microsoft Word Document format - the default format starting with Word 2007.

  • DOC

    Microsoft Word 97-2003 format.

  • DOCM

    Microsoft Office Open XML Macro-Enabled Document format.

  • DOT

    Microsoft Word 97-2003 Template format.

  • DOTM

    Microsoft Office Open XML Macro-Enabled Template format.

  • DOTX

    Microsoft Office Open XML Template format.

  • WordML

    Microsoft Office Word 2003 XML format.

  • OpenDocument

    OpenDocument Text (implemented by the OpenOffice.org office suite)

  • HTML

    Web Page format.

  • MHTML

    Web page archive format.

  • XML

    - Microsoft Word XML Document format (stored in a flat XML file instead of a ZIP package).

    - Microsoft Word XML Macro-Enabled Document format (stored in a flat XML file instead of a ZIP package).

    - Microsoft Word XML Macro-Enabled Template format (stored in a flat XML file instead of a ZIP package).

    - Microsoft Word XML Template format (stored in a flat XML file instead of a ZIP package).

  • PDF (export only)

Initiate Load and Save Operations in Code

The RichEditDocumentServer ships with the following methods to load or save the document and specify its options. Refer to the following examples section for code samples: Files.

Member Description
RichEditDocumentServer.LoadDocument Loads a document from a file or stream. The document format can be specified by one of the DocumentFormat enumeration values or detected automatically.
RichEditDocumentServer.LoadDocumentTemplate Loads a document template from the specified file or stream so that it cannot be overwritten automatically.
Document.LoadDocument Loads a document from the specified file or stream. Its content can be specified by one of the DocumentFormat enumeration values or is automatically determined based on the file’s format.
SubDocument.InsertDocumentContent Inserts content from the selected range into the current document at the specified position.
SubDocument.AppendDocumentContent Appends content from the selected range.
RichEditDocumentServer.SaveDocument Saves the server’s document to a file or stream and specifies the document’s format.
Document.SaveDocument Saves the document to a file or stream and specifies the document’s format.

How to: Load and Save a Document

The code sample below loads the document from a stream and saves the result to the file.

using DevExpress.XtraRichEdit;
using System.Diagnostics;
using System.Reflection;

using (var wordProcessor = new RichEditDocumentServer())
{
    using (Stream stream = GetResourceStream("MyApplication.Document1.rtf"))
    {
        stream.Seek(0, SeekOrigin.Begin);
        wordProcessor.LoadDocument(stream, DocumentFormat.Rtf);
        stream.Close();
    }

    wordProcessor.SaveDocument("Result.docx", DocumentFormat.OpenXml);
}

Process.Start(new ProcessStartInfo("explorer.exe", "/select," + "Result.docx") { UseShellExecute = true });

static Stream GetResourceStream(string resourceName)
{
    return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}

Refer to the following examples for more code samples:

Note

We do not recommend that you use the DocumentFormat.Undefined field as the SaveDocument or SaveCopy method parameter. Otherwise, the document is saved with invalid format.

Basic Format-Specific API

The table below lists the document formats the RichEditDocumentServer supports, and the API used to set format-specific import and export options. You can specify these options in the RichEditDocumentServer.BeforeImport or RichEditDocumentServer.BeforeExport event handlers.

Format Accessed By Import Options Export Options
Plain Text Document.Text RichEditDocumentImportOptions.PlainText RichEditDocumentExportOptions.PlainText
Rich Text Format Document.RtfText RichEditDocumentImportOptions.Rtf RichEditDocumentExportOptions.Rtf
DOCX Document.OpenXmlBytes RichEditDocumentImportOptions.OpenXml RichEditDocumentExportOptions.OpenXml
DOC Document.DocBytes RichEditDocumentImportOptions.Doc RichEditDocumentExportOptions.Doc
DOCM Document.DocmBytes RichEditDocumentImportOptions.Docm RichEditDocumentExportOptions.Docm
DOT Document.DotBytes RichEditDocumentImportOptions.Dot RichEditDocumentExportOptions.Dot
DOTM Document.DotmBytes RichEditDocumentImportOptions.Dotm RichEditDocumentExportOptions.Dotm
DOTX Document.DotxBytes RichEditDocumentImportOptions.Dotx RichEditDocumentExportOptions.Dotx
WordML Document.WordMLText RichEditDocumentImportOptions.WordML RichEditDocumentExportOptions.WordML
FlatOpc (XML) Document.FlatOpcBytes RichEditDocumentImportOptions.FlatOpc RichEditDocumentExportOptions.FlatOpc
FlatOpcMacroEnabled (XML) Document.FlatOpcMacroEnabledBytes RichEditDocumentImportOptions.FlatOpcMacroEnabled RichEditDocumentExportOptions.FlatOpcMacroEnabled
FlatOpcTemplate (XML) Document.FlatOpcTemplateBytes RichEditDocumentImportOptions.FlatOpcTemplate RichEditDocumentExportOptions.FlatOpcTemplate
FlatOpcMacroEnabledTemplate (XML) Document.FlatOpcMacroEnabledTemplateBytes RichEditDocumentImportOptions.FlatOpcMacroEnabledTemplate RichEditDocumentExportOptions.FlatOpcMacroEnabledTemplate
MHT Document.MhtText RichEditDocumentImportOptions.Mht RichEditDocumentExportOptions.Mht
HTML Document.HtmlText RichEditDocumentImportOptions.Html RichEditDocumentExportOptions.Html
ODT Document.OpenDocumentBytes RichEditDocumentImportOptions.OpenDocument RichEditDocumentExportOptions.OpenDocument
PDF Not Supported PdfExportOptions

Tip

Use the SubDocument‘s Get... methods to retrieve and format a part of the document. Refer to the following topic for details: Positions and Ranges.

How to: Handle BeforeImport and BeforeExport events

Check the following examples section for more information on how to accomplish export-related tasks: Export.

How to: Handle the BeforeImport Event

The code sample below illustrates how to handle RichEditDocumentServer.BeforeImport event for different document formats:

using DevExpress.XtraRichEdit.Import;
//...

private void RichEditDocumentServer_BeforeImport(object sender, BeforeImportEventArgs e)
{
    if (e.DocumentFormat == DocumentFormat.PlainText)
    {
        //Detect plain text encoding automatically:
        ((PlainTextDocumentImporterOptions)e.Options).AutoDetectEncoding = true;
    }

    if (e.DocumentFormat == DocumentFormat.Doc)
    {
        //Disable loading comments added to removed ranges in DOC documents
        ((DocDocumentImporterOptions)e.Options).KeepCommentsForRemovedRanges = false;
    }

    if (e.DocumentFormat == DocumentFormat.Html)
    {
        //Load images synchronously with HTML documents
        ((HtmlDocumentImporterOptions)e.Options).AsyncImageLoading = false;
    }
}

How to: Handle the BeforeExport Event

The code sample below shows how specify export options for different formats in the RichEditDocumentServer.BeforeExport event handler.

private void RichEditDocumentServer_BeforeExport(object sender, BeforeExportEventArgs e)
{
    if (e.DocumentFormat == DocumentFormat.PlainText)
    {
        //Include document fields in the exported plain text:
        PlainTextDocumentExporterOptions plainTextOptions = e.Options as PlainTextDocumentExporterOptions;
        plainTextOptions.ExportHiddenText = true;
        plainTextOptions.FieldCodeEndMarker = ">";
        plainTextOptions.FieldCodeStartMarker = "[<";
        plainTextOptions.FieldResultEndMarker = "]";
    }

    if (e.DocumentFormat == DocumentFormat.OpenXml)
    {
        //Specify what DOCX document properties to export:
        OpenXmlDocumentExporterOptions docxOptions = e.Options as OpenXmlDocumentExporterOptions;
        docxOptions.ExportedDocumentProperties = DocumentPropertyNames.Title | DocumentPropertyNames.LastModifiedBy | DocumentPropertyNames.Modified;
    }

    if (e.DocumentFormat == DocumentFormat.Html)
    {
        //Specify HTML export options:
        HtmlDocumentExporterOptions htmlOptions = e.Options as HtmlDocumentExporterOptions;
        htmlOptions.EmbedImages = true;
        htmlOptions.CssPropertiesExportType = CssPropertiesExportType.Style;
        htmlOptions.UseFontSubstitution = false;
    }
}

Notes on Document Content

Control Document Elements on Load

Use the RichEditControlOptionsBase.DocumentCapabilities property to access the DocumentCapabilitiesOptions properties to control what document elements to import. Specify the options in the RichEditDocumentServer.BeforeImport event handler, as shown below. The restricted elements are lost when the document is saved.

static void RichEditDocumentServer_BeforeImport(object sender, BeforeImportEventArgs e) {
    var wordProcessor = sender as RichEditDocumentServer;

    DocumentCapabilitiesOptions capabilityOptions = wordProcessor.Options.DocumentCapabilities;
    capabilityOptions.CharacterFormatting = DocumentCapability.Disabled;
    capabilityOptions.Comments = DocumentCapability.Disabled;
    capabilityOptions.InlineShapes = DocumentCapability.Disabled;
}

HTML Tag Support

The RichEditDocumentServer parses and transforms loaded HTML documents into the internal document model. However, not every HTML tag can be converted into a corresponding document model element. Refer to the HTML Tag Support topic for a list of supported tags.

See Also