wpf-116727-controls-and-libraries-rich-text-editor-examples-files-how-to-load-a-document.md
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.
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:
<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
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.
// Load document from file
private void btnLoadFile_Click(object sender, EventArgs e)
{
richEditControl1.LoadDocument("Document1.rtf",DocumentFormat.Rtf);
}
' 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
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:
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);
stream.Close();
}
}
static Stream GetResourceStream(string resourceName)
{
return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
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
The following code snippet displays the word “Test” in blue.
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;
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:
| 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