Document Management
- 2 minutes to read
Users can use the commands on the File ribbon tab to manage documents.
The sections below provide information on how to invoke the same commands from code.
Create a Document
Call the newDocument method to create a new document.
richEdit.newDocument();
Open a Document
Call the openDocument method to open a document.
- Call the method with parameters to open a particular document.
var documentAsBase64 = "UEsDBAoAAAAAACF<...>DAAA8NQAAAAA=" richEdit.openDocument(documentAsBase64, "DocumentName.docx", DevExpress.RichEdit.DocumentFormat.OpenXml)
- Call the method without parameters to invoke the Open dialog.
richEdit.openDocument();
Save a Document
Note
If the document is not saved in the client-side or server-side event handler, the Save button and a call to the saveDocument method have no effect.
When a user clicks the Save button or the saveDocument method is called, the Rich Text Editor converts the document’s content to base64 and invokes the Saving event. Write the event handler to save the document on the client side. Set the handled property to true to prevent further processing.
Tip
Use the documentSaveFormat property to specify a format in which the document will be converted to base64.
@(Html.DevExpress().RichEdit("richEdit")
...
.OnSaving(
"function(s, e) {" +
"e.handled = true;" +
"console.log(e.base64);" +
"console.log(e.fileName);" +
"console.log(e.format);" +
"}")
...
If the handled property is not set to true (the event is not handled), specify the ExportUrl(String) property to save the document on the server side.
@(Html.DevExpress().RichEdit("richEdit")
.ExportUrl(Url.Action("Export"))
.Height(600)
.ConfirmOnLosingChanges(c => c.Enabled(false))
)
public IActionResult Export(string base64, string fileName, DevExpress.AspNetCore.RichEdit.DocumentFormat format, string reason) {
byte[] fileContents = System.Convert.FromBase64String(base64);
return Ok();
}
Note
If you apply the [ApiController] attribute to your controller, you should add the [FromForm] attribute to parameters in the Export action.
public IActionResult Export([FromForm]string base64, [FromForm]string fileName, [FromForm]int format, [FromForm]string reason) {
byte[] fileContents = System.Convert.FromBase64String(base64);
return Ok();
}
If document saving is implemented on the server-side, you can write the Saved event handler to perform custom actions after a server sends a response to a request for saving.
Important
Learn how to use the ExportUrl method in a Razor Pages web app.
Download a Document
Call the downloadDocument method to download a document.
//downloads the myDocument.txt file
richEdit.downloadDocument(DevExpress.RichEdit.DocumentFormat.PlainText, "myDocument");
Print a Document
Call the printDocument method to invoke the Print dialog.
richEdit.printDocument();