Import and Export

  • 6 minutes to read

This document describes how to load and save documents in different formats. It consists of the following sections:

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 office suite)

  • HTML

    Web Page format.


    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 provides the following methods to load or save the document and specify its options. Refer to the Files examples section for code samples.

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. Refer to the How to: Load a Document and How to: Save a Document examples for more code samples.

RichEditDocumentServer server = new RichEditDocumentServer();

using (Stream stream = GetResourceStream("MyApplication.FirstLook.docx"))
    stream.Seek(0, SeekOrigin.Begin);
    server.LoadDocument(stream, DocumentFormat.OpenXml);
    server.SaveDocument("Result.docx", DocumentFormat.OpenXml);
    System.Diagnostics.Process.Start("explorer.exe", "/select," + "Result.docx");

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


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

How to: Handle BeforeImport and BeforeExport events


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

How to: Handle the BeforeImport Event

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

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

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

    if (e.DocumentFormat == DocumentFormat.Html)
        //Load images synchronously with HTML documents
        ((DevExpress.XtraRichEdit.Import.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 RichEditDocument_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.

private void Server_BeforeImport(object sender, BeforeImportEventArgs e)
    RichEditDocumentServer server = sender as RichEditDocumentServer;

    server.Options.DocumentCapabilities.CharacterFormatting = DocumentCapability.Disabled;
    server.Options.DocumentCapabilities.Comments = DocumentCapability.Disabled;
    server.Options.DocumentCapabilities.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