Back to Devexpress

How to: Load a Document

wpf-116727-controls-and-libraries-rich-text-editor-examples-files-how-to-load-a-document.md

latest6.5 KB
Original Source

How to: Load a Document

  • Dec 23, 2025
  • 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:

xaml
<Grid>
    <dxre:RichEditControl CommandBarStyle="Ribbon"
                          DocumentSource="pack://application:,,,/WpfApplication1;component/Document.docx"/>
</Grid>

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

Load a Document from File

Warning

Using file paths sourced from untrusted input may expose unauthorized files or allow unintended file access. Always validate and normalize all external paths to prevent path manipulation.

csharp
// Load document from file
private void btnLoadFile_Click(object sender, EventArgs e)
{
    richEditControl1.LoadDocument("Document1.rtf",DocumentFormat.Rtf);
}
vb
' Load document from file
Private Sub btnLoadFile_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles btnLoadFile.Click
    richEditControl1.LoadDocument("Document1.rtf")
End Sub

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:

  • DOC, DOCX, RTF, HTM, HTML, MHT, XML, EPUB;
  • ODT - non-encrypted files only;

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

csharp
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);
        stream.Close();
    }
}
static Stream GetResourceStream(string resourceName)
{
    return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
vb
Imports System.IO
Imports DevExpress.Xpf.RichEdit
Imports DevExpress.XtraRichEdit.API.Native
Imports System.Reflection
' ...
' Load document from stream
Private Sub btnLoadStream_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles btnLoadStream.Click
    Using stream As Stream = GetResourceStream("MyApplication.Document1.rtf")
        stream.Seek(0, SeekOrigin.Begin)
        richEditControl1.LoadDocument(stream, DocumentFormat.Rtf)
        stream.Close()
    End Using
End Sub
Private Shared Function GetResourceStream(ByVal resourceName As String) As Stream
    Return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
End Function
' Load document from file
Private Sub btnLoadFile_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles btnLoadFile.Click
    richEditControl1.LoadDocument("Document1.rtf",DocumentFormat.Rtf)
End Sub

Load a Document from a String

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

View Example

csharp
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;
vb
Dim rtfString As String = "{\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:

EventDescription
RichEditControl.BeforeImportOccurs before a document is loaded (imported from an external source).
RichEditControl.InitializeDocumentOccurs before a document is loaded. Handle this event to set initial document settings.
RichEditControl.DocumentLoadedOccurs after a document is loaded into the RichEdit control.
DocumentLayout.DocumentFormattedFires after the document layout is calculated.
RichEditControl.InvalidFormatExceptionFires when the supplied data could not be recognized as data in the assumed format for import.
RichEditControl.UnhandledExceptionThis event is raised when an exception unhandled by the RichEditControl occurs.

See Also

Troubleshooting