Skip to main content

Create and Open a Document

  • 5 minutes to read

Call the New() method to create a document. The ASPxRichEdit sets the document’s DocumentId to a randomly generated GUID value.

The Open method overloads allow you to open documents from a file system, stream, or byte array.

View Example: How to save/load documents to/from a database

Open a Document from a File System

Call the Open(String) method to open a document from the server’s file system. When you open a document for the first time, the control loads it to the server’s memory and generates a DocumentId from the pathToDocument parameter value.

If the document is already open, the method activates the document stored in the server’s memory.

string pathToDocument = Server.MapPath("~/App_Data/Documents/doc.rtf");
ASPxRichEdit1.Open(pathToDocument);

Specify the WorkDirectory property to enable the Open and Save As UI commands. Users cannot access the file system outside the specified directory.

Open a Document from a Stream

Call the Open(String, DocumentFormat, Func<Stream>) method to load a document from a stream.

Specify the documentId parameter to identify the document. If the specified value is not unique within all open documents, the method activates the previously loaded document with the same documentId and ignores the stream data.

private void CustomDocumentOpening() {
    var uniqueDocumentId = Guid.NewGuid().ToString();
    using (var stream = GetStreamFromCustomDocumentStorage()) {
        ASPxRichEdit1.Open(uniqueDocumentId, DocumentFormat.Rtf, () => stream);
    }
}
private FileStream GetStreamFromCustomDocumentStorage() {
    // Provide your custom logic to obtain a document (for instance, from a database) 
}

Open a Document from a Byte Array

Call the Open(String, DocumentFormat, Func<Byte[]>) method to load a document from a byte array.

Specify the documentId parameter to identify the document. If the specified value is not unique within all open documents, the method activates the previously loaded document with the same documentId and ignores the byte array.

private void CustomDocumentOpening() {
    var uniqueDocumentId = Guid.NewGuid().ToString();
    byte[] documentContentAsByteArray = GetByteArrayFromCustomDocumentStorage();
    using (var stream = GetStreamFromCustomDocumentStorage()) {
        ASPxRichEdit1.Open(uniqueDocumentId, DocumentFormat.Rtf, () => documentContentAsByteArray);
    }
}
private byte[] GetByteArrayFromCustomDocumentStorage() {
    // Provide your custom logic to obtain a document (for instance, from a database) 
}

Open a Document as a Template

ASPxRichEdit allows users to open their own copies of a document in its initial state. In this case, the original version of a document is used as a template. Each user can obtain a copy and modify it independently from other users who open the same template.

To implement this behavior, you need a template document. Pass it as a parameter to one of the Open method overloads listed below. Each method has the documentId parameter, so you can specify unique documentId values for each user. A called method opens an independent copy of the template document for each user as a result.

  • Open(String, DocumentFormat, Func<Stream>)

    var templatePath = Server.MapPath("~/App_Data/WorkDirectory/Document.rtf");
    var documentId = Guid.NewGuid().ToString();
    using (var stream = new FileStream(templatePath, FileMode.Open)) {
        ASPxRichEdit1.Open(documentId, DocumentFormat.Rtf, () => stream);
    }
    
  • Open(String, DocumentFormat, Func<Byte[]>)

    var templatePath = Server.MapPath("~/App_Data/WorkDirectory/Document.rtf");
    var documentId = Guid.NewGuid().ToString();
    ASPxRichEdit1.Open(documentId, DocumentFormat.Rtf, () => File.ReadAllBytes(templatePath));
    

Do not use the Open(String) method for this scenario, because the method always opens a single document that has the same DocumentId equal to the pathToDocument parameter.

Switch Between Open Documents

When a user opens a document, the ASPxRichEdit retains the previous document in the server memory. The DocumentManager maintains information about every open document. Call the following methods to access this information:

FindDocument(String)
Returns a RichEditDocumentInfo object that contains information about a document with the specified DocumentId.
GetAllDocuments()
Returns a collection of RichEditDocumentInfo objects that contain information about every open document.

Call the Open(RichEditDocumentInfo) method to activate a previously opened document.

var documentInfo = (RichEditDocumentInfo)DocumentManager.FindDocument("myDocumentId");
ASPxRichEdit1.Open(documentInfo);

Refresh a Document

When you open a document from a file system and then try to reopen it, the ASPxRichEdit activates the previously opened document from the document storage.

If the document was modified by a third-party tool (Microsoft Excel, for instance), the ASPxRichEdit does not reflect these changes. Call the CloseDocument(String) method to explicitly unload the document from the document storage. Then load the modified version of the document.

var filePath = ASPxRichEdit1.DocumentId;
DocumentManager.CloseDocument(filePath);
ASPxRichEdit1.Open(filePath);