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 or RichEditDocumentServer.RtfText property. Handle related events to determine a moment when the document can be modified.
Load a Document from a File
Note
A complete sample project is available at https://github.com/DevExpress-Examples/word-processing-document-api-examples-t418535
server.LoadDocument("Documents\\Grimm.docx", DocumentFormat.OpenXml);
Load a Document from a Stream
using System.IO;
using DevExpress.XtraRichEdit;
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);
richEditDocumentServer1.LoadDocument(stream, DocumentFormat.Rtf);
stream.Close();
}
}
static Stream GetResourceStream(string resourceName) {
return Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName);
}
Load a Document from a 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;
Related Events
The table below lists events related to the document loading:
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. |