Skip to main content
A newer version of this page is available. .

How to: Load a Document

  • 2 minutes to read

RichEditDocumentServer allows you to load a document from a file a data stream or a string using the RichEditDocumentServer.LoadDocument method. Handle related events to determine a moment when the document can be modified.

Load a Document from a File

server.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);

Load a Document from a Stream

The format of the document loaded from a stream is detected automatically by the built-in DevExpress.XtraSpreadsheet.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.


using System.IO;
using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.API.Native;
using System.Reflection;
// ...
// 
// Load document from stream
using(Stream stream = GetResourceStream("MyApplication.Document1.rtf"))
{
    stream.Seek(0, SeekOrigin.Begin);
    richEditDocumentServer1.LoadDocument(stream, DocumentFormat.Rtf);
    stream.Close();
}
static Stream GetResourceStream(string resourceName)
{
    return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}

Load a Document from a String

Use one of the following properties to load a string in the specific format:

The code sample below load an RTF string:


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 document = server.Document;
document.RtfText = rtfString;

The table below lists events related to the document load:

Event Description
RichEditDocumentServer.BeforeImport Occurs before a document is loaded (imported from an external source).
RichEditDocumentServer.InitializeDocument Occurs before a document is loaded. Handle this event to set initial document settings.
RichEditDocumentServer.DocumentLoaded Occurs after a document is loaded.
DocumentLayout.DocumentFormatted Fires after the document layout is calculated.
RichEditDocumentServer.InvalidFormatException Fires when the supplied data could not be recognized as data in the assumed format for import.
RichEditDocumentServer.UnhandledException This event is raised when an exception unhandled by the RichEditDocumentServer occurs.