Skip to main content

How to: Load a Document into the RichEditControl

  • 2 minutes to read

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.

Load a Document from a File

View Example

richEditControl1.LoadDocument("Grimm.docx");

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, DOCM, DOTX, DOT, DOTM, DOCX, RTF, HTM, HTML, MHT, XML, FlatOpc, EPUB;
  • ODT - non-encrypted files only.

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);
}

Load a Document from a String

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}";

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