windowsforms-5887-controls-and-libraries-rich-text-editor-examples-files-how-to-load-a-document-into-the-rich-edit-control.md
RichEditControl allows you to load a document from a file, data stream, byte array, or a string. Handle one of the related events to determine the moment when the document can be modified.
richEditControl1.LoadDocument("Grimm.docx");
richEditControl1.LoadDocument("Grimm.docx")
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 RichEditControl.LoadDocument method overloads with explicit format definition to improve performance.
View Example: How to load a document into the RichEditControl
using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("LoadDocumentExample.TextWithImagesODT"))
{
richEditControl1.LoadDocument(stream);
}
Using stream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("LoadDocumentExample.TextWithImagesODT")
richEditControl1.LoadDocument(stream)
End Using
This code snippet uses the RichEditControl.RtfText property to load a document from a string in RTF format. It displays the word “Test” in blue.
richEditControl1.RtfText = @"{\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}";
richEditControl1.RtfText = "{\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}"
Events related to document loading are listed in the following table:
| 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 unhandled exception of the RichEditControl occurs. |
See Also
Import an HTML File into the Rich Text Editor or Export a Document to HTML