Skip to main content

Import and Export Rich Text Documents

  • 6 minutes to read

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

Built-in UI

End users can click the Open button on the File ribbon tab to invoke the Open dialog. Select the file you want to open and click Open.


The Save and Save As buttons on the File ribbon tab allow users to save the changes made to the current document or save it as a new file. Click the Save As button to invoke the Save As dialog used to save a copy as a new file, specify the document’s location, name, and format. Refer to the How to: Create the RichEditControl with a Ribbon UI for details on how to provide a ribbon UI for the RichEditControl.


The DocumentSaveOptions.CurrentFileName property returns the current document’s file name (including the path and extension).


You can prohibit end users from creating, loading or saving documents. Set the RichEditBehaviorOptions.CreateNew, RichEditBehaviorOptions.Open, RichEditBehaviorOptions.Save or RichEditBehaviorOptions.SaveAs property to DocumentCapability.Disabled to disable or hide the corresponding commands in the Ribbon UI and pop-up menu.

Initiate Load and Save Operations in Code

The RichEditControl 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
RichEditControl.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.
RichEditControl.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.
RichEditControl.SaveDocument Saves the control’s document to a file or stream and specifies the document’s format.
RichEditControl.SaveDocumentAs Invokes the Save As dialog and saves a document.
Document.SaveDocument Saves the document to a file or stream and specifies the document’s format.


The SaveDocumentCommand and SaveDocumentAsCommand execution sets the RichEditControl.Modified property to false. The RichEditControl.SaveDocument, RichEditControl.SaveDocumentAs or Document.SaveDocument method call does not automatically change the Modified property value.

How to: Load and Save a Document

The code sample below loads the document from a file stream when the form is opened and saves the result to the file when the form is closed. Refer to the How to: Load a Document and How to: Save a Document examples for more code samples.

private void Form1_Load(object sender, EventArgs e)
using (Stream stream = new FileStream("FirstLook.docx", FileMode.Open))
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
   richEditControl.SaveDocument("Result.docx", DocumentFormat.OpenXml);


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

Basic Format-Specific API

The table below lists document formats the RichEditControl supports, and the API used to set format-specific import and export options. You can specify these options in the RichEditControl.BeforeImport or RichEditControl.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 more information.

How to: Handle the BeforeImport Event


Check the Import and Export examples section for more information on how to accomplish an import- or export-related task.

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

private void RichEditControl_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 RichEditControl.BeforeExport event handler.

private void RichEditControl_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