Document Management

  • 4 minutes to read

Commands on the File ribbon tab can be used to manage documents.

File Tab

The sections below provide information on how to invoke these 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.

@(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 save request.

Learn how to use the ExportUrl method in a Razor Pages web app.

Export a Document to PDF

The exportToPdf method exports the current document to the portable document format (PDF) and invokes the PdfExporting event for further processing on the client side. Use the base64, blob, or blobStream event parameter to access the processed PDF document. You can set the handled property to true to prevent further processing on the server.

@(Html.DevExpress().RichEdit("richEdit")
  .OnPdfExporting(
    @"function(s, e) {
    e.handled = true;
    console.log(e.base64);
    }")

If the handled property is set to false or undefined, specify the ExportUrl(String) property to process the document on the server.

@(Html.DevExpress().RichEdit("richEdit")
  .Pdf(p => {
      p.ExportUrl(Url.Action("Export"));
  })
  .Fonts(f =>
  //...
)
public IActionResult Export(string base64, string fileName) {
    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) {
  byte[] fileContents = System.Convert.FromBase64String(base64);
  return Ok();
}

If document processing is implemented on the server, you can write the PdfExported event handler to perform custom actions after a server sends a response to a request for document processing.

IMPORTANT

When you export to PDF, you should upload a list of available fonts and their sources to the client and register the pdfkit library on your page.

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");

To download the document in a portable document format (PDF), call the downloadPdf method.

Call the printDocument method to invoke the Print dialog.

richEdit.printDocument(DevExpress.RichEdit.PrintMode.Pdf);

If the mode parameter is not specified, the RichEdit prints a document in a mode set by the Mode(PrintMode) method on the server side or printMode property on the client side.

The RichEdit control supports HTML and PDF print modes:

  • HTML Mode (default). The control renders the current document's markup in a blank browser tab and calls the browser's Print dialog. To prevent content offset, ensure the document and printer use the same page format.

  • PDF Mode. The control exports the current document to PDF and invokes the Print dialog for the PDF file. When you export to PDF, you should upload a list of available fonts and their sources to the client and register the pdfkit library on your page.

See Also