How to: Load a Document

  • 4 minutes to read

The RichEditDocumentServer.LoadDocument method allows you to load a document from a file, stream, byte array, or string. Handle related events to determine the moment when the document can be modified.

The format of the loaded document is detected automatically by the built-in IFormatDetectorService service implementation. The following formats are detected:

  • ODT - non-encrypted files only.

Use the LoadDocument method overloads with explicit format definition to improve performance.

Load a Document from a File


A complete sample project is available in the following repository:

wordProcessor.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);

Load a Document from a Stream

using System.IO;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Reflection;
// ...
// Load document from a stream
using(Stream stream = GetResourceStream("MyApplication.Document1.rtf"))
    stream.Seek(0, SeekOrigin.Begin);
    wordProcessor.LoadDocument(stream, DocumentFormat.Rtf);
static Stream GetResourceStream(string resourceName)
    return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

Load a Document from a String

The following properties load strings in various formats:

The code sample below loads an RTF string:

using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
    string rtfString = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1049
{\fonttbl{\f0\fswiss\fprq2\fcharset0 Arial;}
{\f1\fswiss\fcharset0 Arial;}}
{\colortbl ;\red0\green0\blue255;}
\viewkind4\uc1\pard\cf1\lang1033\b\f0\fs32 Test.\cf0\b0\f1\fs20\par}";
    Document document = wordProcessor.Document;
    document.RtfText = rtfString;

Asynchronous Load

Use the RichEditDocumentServerExtensions.LoadDocumentAsync method to asynchronously load a document from a file, stream, or byte array.

To run asynchronous operations, you need to use the RichEditDocumentServerExtensions class from the DevExpress.Docs.v20.2.dll assembly. Remember to add this assembly to your project. If you use this assembly in production code, a license for the DevExpress Office File API or DevExpress Universal Subscription is required. For pricing information, refer to the following page: DevExpress Subscription Plans.


Take into account the following when you call this method:

  • The events fired by this method's call may occur in a different thread than the target operation.

  • The operation is not thread safe (documents should not be accessed simultaneously by different threads). Wait until the operation is completed before working with the document, i.e., use the await operator.

The code sample below merges two asynchronously loaded documents and asynchronously exports the result:

private async void MergeDocuments()
  using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
  using (RichEditDocumentServer wordProcessor2 = new RichEditDocumentServer())
      await Task.WhenAll(new Task[]
      await wordProcessor.SaveDocumentAsync("merged.docx", DocumentFormat.OpenXml);

The table below lists events related to the document load:

Event Description
RichEditDocumentServer.BeforeImport Occurs before a document is loaded (imported from an external source).
RichEditDocumentServer.InitializeDocument Occurs before a document is loaded. Handle this event to set initial document settings.
RichEditDocumentServer.DocumentLoaded Occurs after a document is loaded.
DocumentLayout.DocumentFormatted Fires after the document layout is calculated.
RichEditDocumentServer.InvalidFormatException Fires when the supplied data could not be recognized as data in the assumed format for import.
RichEditDocumentServer.UnhandledException This event is raised when an exception unhandled by the RichEditDocumentServer occurs.