Back to Devexpress

Import and Export Word Processing Documents

officefileapi-15441-word-processing-document-api-import-and-export.md

latest19.1 KB
Original Source

Import and Export Word Processing Documents

  • Jan 16, 2026
  • 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

  • RTF

  • DOCX

  • DOC

  • DOCM

  • DOT

  • DOTM

  • DOTX

  • WordML

  • OpenDocument

  • HTML

  • MHTML

  • XML

  • 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.

MemberDescription
RichEditDocumentServer.LoadDocumentLoads a document from a file or stream. The document format can be specified by one of the DocumentFormat enumeration values or detected automatically.
RichEditDocumentServer.LoadDocumentTemplateLoads a document template from the specified file or stream so that it cannot be overwritten automatically.
Document.LoadDocumentLoads 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.InsertDocumentContentInserts content from the selected range into the current document at the specified position.
SubDocument.AppendDocumentContentAppends content from the selected range.
RichEditDocumentServer.SaveDocumentSaves the server’s document to a file or stream and specifies the document’s format.
Document.SaveDocumentSaves the document to a file or stream and specifies the document’s format.

How to: Load and Save a Document

The following code snippet loads the document from a stream and saves the result to the file.

csharp
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.Docx);
}

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

static Stream GetResourceStream(string resourceName)
{
    return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
vb
Imports DevExpress.XtraRichEdit
Imports System.Diagnostics
Imports System.Reflection

Using wordProcessor = New RichEditDocumentServer()
    Using stream As Stream = GetResourceStream("MyApplication.Document1.rtf")
        stream.Seek(0, SeekOrigin.Begin)
        wordProcessor.LoadDocument(stream, DocumentFormat.Rtf)
        stream.Close()
    End Using

    wordProcessor.SaveDocument("Result.docx", DocumentFormat.Docx)
End Using

Process.Start(New ProcessStartInfo("explorer.exe", "/select," & "Result.docx") With {.UseShellExecute = True})

Shared Function GetResourceStream(ByVal resourceName As String) As Stream
    Return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
End Function

Refer to the following examples for more code samples:

Note

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 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.

FormatAccessed ByImport OptionsExport Options
Plain TextDocument.TextRichEditDocumentImportOptions.PlainTextRichEditDocumentExportOptions.PlainText
Rich Text FormatDocument.RtfTextRichEditDocumentImportOptions.RtfRichEditDocumentExportOptions.Rtf
DOCXDocument.OpenXmlBytesRichEditDocumentImportOptions.OpenXmlRichEditDocumentExportOptions.OpenXml
DOCDocument.DocBytesRichEditDocumentImportOptions.DocRichEditDocumentExportOptions.Doc
DOCMDocument.DocmBytesRichEditDocumentImportOptions.DocmRichEditDocumentExportOptions.Docm
DOTDocument.DotBytesRichEditDocumentImportOptions.DotRichEditDocumentExportOptions.Dot
DOTMDocument.DotmBytesRichEditDocumentImportOptions.DotmRichEditDocumentExportOptions.Dotm
DOTXDocument.DotxBytesRichEditDocumentImportOptions.DotxRichEditDocumentExportOptions.Dotx
WordMLDocument.WordMLTextRichEditDocumentImportOptions.WordMLRichEditDocumentExportOptions.WordML
FlatOpc (XML)Document.FlatOpcBytesRichEditDocumentImportOptions.FlatOpcRichEditDocumentExportOptions.FlatOpc
FlatOpcMacroEnabled (XML)Document.FlatOpcMacroEnabledBytesRichEditDocumentImportOptions.FlatOpcMacroEnabledRichEditDocumentExportOptions.FlatOpcMacroEnabled
FlatOpcTemplate (XML)Document.FlatOpcTemplateBytesRichEditDocumentImportOptions.FlatOpcTemplateRichEditDocumentExportOptions.FlatOpcTemplate
FlatOpcMacroEnabledTemplate (XML)Document.FlatOpcMacroEnabledTemplateBytesRichEditDocumentImportOptions.FlatOpcMacroEnabledTemplateRichEditDocumentExportOptions.FlatOpcMacroEnabledTemplate
MHTDocument.MhtTextRichEditDocumentImportOptions.MhtRichEditDocumentExportOptions.Mht
HTMLDocument.HtmlTextRichEditDocumentImportOptions.HtmlRichEditDocumentExportOptions.Html
ODTDocument.OpenDocumentBytesRichEditDocumentImportOptions.OpenDocumentRichEditDocumentExportOptions.OpenDocument
PDFNot SupportedPdfExportOptions

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 following code snippet handles the RichEditDocumentServer.BeforeImport event for different document formats:

csharp
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;
    }
}
vb
Imports DevExpress.XtraRichEdit.Import

Private Sub RichEditDocumentServer_BeforeImport(ByVal sender As Object, ByVal e As BeforeImportEventArgs)
    If e.DocumentFormat = DocumentFormat.PlainText Then
        'Detect plain text encoding automatically:
        (CType(e.Options, PlainTextDocumentImporterOptions)).AutoDetectEncoding = True
    End If

    If e.DocumentFormat = DocumentFormat.Doc Then
        'Disable loading comments added to removed ranges in DOC documents
        (CType(e.Options, DocDocumentImporterOptions)).KeepCommentsForRemovedRanges = False
    End If

    If e.DocumentFormat = DocumentFormat.Html Then
        'Load images synchronously with HTML documents
        (CType(e.Options, HtmlDocumentImporterOptions)).AsyncImageLoading = False
    End If
End Sub

How to: Handle the BeforeExport Event

The following code snippet specifies export options for different formats in the RichEditDocumentServer.BeforeExport event handler.

csharp
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.Docx)
    {
        //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;
    }
}
vb
Private Sub RichEditDocumentServer_BeforeExport(ByVal sender As Object, ByVal e As BeforeExportEventArgs)
    If e.DocumentFormat = DocumentFormat.PlainText Then
        'Include document fields in the exported plain text:
        Dim plainTextOptions As PlainTextDocumentExporterOptions = TryCast(e.Options, PlainTextDocumentExporterOptions)
        plainTextOptions.ExportHiddenText = True
        plainTextOptions.FieldCodeEndMarker = ">"
        plainTextOptions.FieldCodeStartMarker = "[<"
        plainTextOptions.FieldResultEndMarker = "]"
    End If

    If e.DocumentFormat = DocumentFormat.Docx Then
        'Specify what DOCX document properties to export:
        Dim docxOptions As OpenXmlDocumentExporterOptions = TryCast(e.Options, OpenXmlDocumentExporterOptions)
        docxOptions.ExportedDocumentProperties = DocumentPropertyNames.Title Or DocumentPropertyNames.LastModifiedBy Or DocumentPropertyNames.Modified
    End If

    If e.DocumentFormat = DocumentFormat.Html Then
        'Specify HTML export options:
        Dim htmlOptions As HtmlDocumentExporterOptions = TryCast(e.Options, HtmlDocumentExporterOptions)
        htmlOptions.EmbedImages = True
        htmlOptions.CssPropertiesExportType = CssPropertiesExportType.Style
        htmlOptions.UseFontSubstitution = False
    End If
End Sub

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.

csharp
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;
}
vb
Shared Sub RichEditDocumentServer_BeforeImport(ByVal sender As Object, ByVal e As BeforeImportEventArgs)
    Dim wordProcessor = TryCast(sender, RichEditDocumentServer)

    Dim capabilityOptions As DocumentCapabilitiesOptions = wordProcessor.Options.DocumentCapabilities
    capabilityOptions.CharacterFormatting = DocumentCapability.Disabled
    capabilityOptions.Comments = DocumentCapability.Disabled
    capabilityOptions.InlineShapes = DocumentCapability.Disabled
End Sub

Manage Documents with Right-to-Left Content

Word Processing Document API supports right-to-left direction in main document body and complex elements (tables, comments, and shapes). To process the text direction in the RTF files correctly, make sure that the required content’s text direction is explicitly specified (that is, it is enclosed in the \rtlch tag).

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

Use Word Processing Document API to Load HTML Files or Export Documents to HTML

Export to PDF

Word Processing Document API Examples