Back to Devexpress

How to: Save a Document

officefileapi-116815-word-processing-document-api-examples-files-how-to-save-a-document.md

latest8.3 KB
Original Source

How to: Save a Document

  • Feb 21, 2025
  • 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

Take into account the following when you save the document:

Save to a File

View Example

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

Save to a Stream

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

wordProcessor.SaveDocument(new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite), DocumentFormat.Docx);
System.Diagnostics.Process.Start("explorer.exe", "/select," + "SavedDocument.docx");
vb
wordProcessor.Document.AppendDocumentContent("Documents//Grimm.docx", DocumentFormat.Docx)

wordProcessor.SaveDocument(New FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite), DocumentFormat.Docx)
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.

csharp
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;
             }
         }
     }
}
vb
Private Sub server_BeforeExport(ByVal sender As Object, ByVal e As DevExpress.XtraRichEdit.BeforeExportEventArgs) Handles server.BeforeExport
    Dim checkDocument As DocumentExportCapabilities = server.Document.RequiredExportCapabilities
    If (e.DocumentFormat = DocumentFormat.Rtf) AndAlso checkDocument.InlinePictures Then
        Dim reduceFileSize As DialogResult = MessageBox.Show("This document contains inline pictures." & Constants.vbLf & "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." & Constants.vbLf & "Enable dual picture format in a saved file?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)

        Dim options As RtfDocumentExporterOptions = TryCast(e.Options, RtfDocumentExporterOptions)
        If options IsNot Nothing Then
            Select Case reduceFileSize
            Case System.Windows.Forms.DialogResult.Yes
                    options.Compatibility.DuplicateObjectAsMetafile = True
            Case System.Windows.Forms.DialogResult.No
                    options.Compatibility.DuplicateObjectAsMetafile = False
            End Select
        End If
    End If
End Sub

Asynchronous Save

Important

The RichEditDocumentServerExtensions class is defined in the DevExpress.Docs.v25.2.dll assembly and DevExpress.Document.Processor NuGet package. Add this assembly or package 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:

csharp
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.Docx);
  }
}
vb
Private Async Sub MergeDocuments()
    Using wordProcessor As RichEditDocumentServer = New RichEditDocumentServer()

        Using wordProcessor2 As RichEditDocumentServer = 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.Docx)
        End Using
    End Using
End Sub