Skip to main content
A newer version of this page is available. .

Import and Export

  • 5 minutes to read

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

Supported Formats

The RichEditDocumentServer supports the following document formats:

  • Plain Text;
  • RTF (Rich Text Format);
  • DOCX (MS Office Word 2007 -… format);
  • DOC (Microsoft Word 97-2003 format);
  • WordML (MS Office Word 2003 XML format);
  • OpenDocument (implemented by the OpenOffice.org office suite);
  • HTML;
  • MHTML (web page archive format);
  • PDF format (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);
    stream.Close();
}
    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
WordML Document.WordMLText RichEditDocumentImportOptions.WordML RichEditDocumentExportOptions.WordML
MHT Document.MhtText RichEditDocumentImportOptions.Mht RichEditDocumentExportOptions.Mht
HTML Document.HtmlText RichEditDocumentImportOptions.Html RichEditDocumentExportOptions.Html
PDF Not Supported PdfExportOptions

Tip

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

Tip

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