Skip to main content

How to: Load a Document

  • 3 minutes to read

DXRichEdit allows you to load a document from a file, data stream, byte array or a string in code and in XAML.

Determine a moment when the document can be modified using related events.

Load a Document in XAML

Use the RichEditControl.DocumentSource property to bind the DXRichEdit to the document source in XAML. Documents can be loaded from a stream, byte array or from a location specified by the full file path or Uri. An empty document is created if the RichEditControl.DocumentSource property is null.

The following code snippet uses a pack Uri as a document source to load a sample document:

View Example

    <dxre:RichEditControl CommandBarStyle="Ribbon" 

This approach does not save changes made to the document back to database. Refer to the Lesson 4 - Bind the RichEditControl to a Document Source using the MVVM pattern for information on how to save changes.

Load a Document from File

// Load document from file
private void btnLoadFile_Click(object sender, EventArgs e)

Load a Document from a Stream

The format of the document loaded from a stream is detected automatically by the built-in DevExpress.XtraRichEdit.Services.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.

using System.IO;
using DevExpress.Xpf.RichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Reflection;
// ...
// Load document from stream
private void btnLoadStream_Click(object sender, EventArgs e)
    using(Stream stream = GetResourceStream("MyApplication.Document1.rtf"))
        stream.Seek(0, SeekOrigin.Begin);
        richEditControl1.LoadDocument(stream, DocumentFormat.Rtf);
static Stream GetResourceStream(string resourceName)
    return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);

Load a Document from a String

The following code snippet displays the word “Test” in blue.

View Example

            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.RtfText = rtfString;

The table below lists events related to the document load:

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