Back to Devexpress

How to: Load a Document into the RichEditControl

windowsforms-5887-controls-and-libraries-rich-text-editor-examples-files-how-to-load-a-document-into-the-rich-edit-control.md

latest4.6 KB
Original Source

How to: Load a Document into the RichEditControl

  • Nov 28, 2023
  • 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

csharp
richEditControl1.LoadDocument("Grimm.docx");
vb
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

csharp
using (Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("LoadDocumentExample.TextWithImagesODT"))
{
    richEditControl1.LoadDocument(stream);
}
vb
Using stream As Stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("LoadDocumentExample.TextWithImagesODT")
    richEditControl1.LoadDocument(stream)
End Using

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.

csharp
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}";
vb
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:

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 unhandled exception of the RichEditControl occurs.

See Also

Troubleshooting

Import an HTML File into the Rich Text Editor or Export a Document to HTML

IFormatDetectorService