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

How to: Save a Document

  • 4 minutes to read

To save a document in the RichEditDocumentServer, use the RichEditDocumentServer.SaveDocument method. The RichEditControlOptionsBase.DocumentSaveOptions property retrieves information about the current and default file names and formats.

Note

If a document is being saved to a locked or read-only file, subscribe to the RichEditDocumentServer.UnhandledException event and set the RichEditUnhandledExceptionEventArgs.Handled property to true to prevent application failure.

Save to a File

View Example

wordProcessor.Document.AppendDocumentContent("Documents\\Grimm.docx", DocumentFormat.OpenXml);
wordProcessor.SaveDocument("SavedDocument.docx", DocumentFormat.OpenXml); 
    System.Diagnostics.Process.Start("explorer.exe", "/select," + "SavedDocument.docx");

Save to a Stream

wordProcessor.Document.AppendDocumentContent("Documents//Grimm.docx", DocumentFormat.OpenXml);

wordProcessor.SaveDocument(new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite), DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("explorer.exe", "/select," + "SavedDocument.docx");

Specify Export Options

The RichEditDocumentServer.BeforeExport event is raised before you save a document. You can handle this event to check how the exporter formats a document. For example, the following code snippet determines whether or not a document contains complex formatting elements (inline pictures). If these elements are found, the user is prompted to save the metafile representation along with the original picture.

using DevExpress.XtraRichEdit;
using DevExpress.XtraRichEdit.Export;


private static void Server_BeforeExport(object sender, BeforeExportEventArgs e)
{
     DocumentExportCapabilities checkDocument = server.Document.RequiredExportCapabilities;
     if ((e.DocumentFormat == DocumentFormat.Rtf) && checkDocument.InlinePictures)
     {
         DialogResult reduceFileSize = MessageBox.Show("This document contains inline pictures.\n" +
         "You can embed the same picture in two different types (original and Windows Metafile) for better compatibility," +
         " although it increases the file size. By default, a picture is saved in the original format only.\n" +
         "Enable dual picture format in a saved file?",
         "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

         RtfDocumentExporterOptions options = e.Options as RtfDocumentExporterOptions;
         if (options != null)
         {
             switch (reduceFileSize)
             {
                 case DialogResult.Yes:
                     options.Compatibility.DuplicateObjectAsMetafile = true;
                     break;
                 case System.Windows.Forms.DialogResult.No:
                     options.Compatibility.DuplicateObjectAsMetafile = false;
                     break;
             }
         }
     }
}

Asynchronous Save

Important

The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v21.2.dll assembly. Add this assembly to your project to use the RichEditDocumentServerExtensions members. You need a license for the DevExpress Office File API Subscription or DevExpress Universal Subscription to use this library in production code.

Use the RichEditDocumentServerExtensions.SaveDocumentAsync method to asynchronously save a document to a file, stream, or byte array.

Important

Take into account the following when you call this method:

  • The events fired by this method call may occur in a different thread than the target operation.

  • The operation is not thread safe (the document should not be accessed simultaneously by different threads). Wait until the operation is completed before you continue to work with the document (for example, use the await operator).

The code sample below merges two asynchronously loaded documents and asynchronously exports the result:

private async void MergeDocuments()
{
  using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer())
  using (RichEditDocumentServer wordProcessor2 = new RichEditDocumentServer())
  {
      await Task.WhenAll(new Task[]
      {
          wordProcessor.LoadDocumentAsync("Document1.docx"),
          wordProcessor2.LoadDocumentAsync("Document2.docx")
      });
      wordProcessor.AppendDocumentContent(wordProcessor2.Document.Range);
      await wordProcessor.SaveDocumentAsync("merged.docx", DocumentFormat.OpenXml);
  }
}