How to: Save a Document
- 3 minutes to read
To save a document in the RichEditDocumentServer, use the RichEditDocumentServer.SaveDocument method. The RichEditControlOptionsBase.DocumentSaveOptions property provides access to information about the current and default file names and formats.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/word-processing-document-api-examples-t418535
server.Document.AppendDocumentContent("Documents\\Grimm.docx", DocumentFormat.OpenXml);
server.SaveDocument("SavedDocument.docx", DocumentFormat.OpenXml);
System.Diagnostics.Process.Start("explorer.exe", "/select," + "SavedDocument.docx");
Note
Consider the situation when a document is being saved to a locked or read-only file. To prevent application failure, subscribe to the RichEditDocumentServer.UnhandledException event and set the RichEditUnhandledExceptionEventArgs.Handled property to true.
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 they are found, the user is prompted to save the metafile representation along with the original picture (by enabling the RtfDocumentExporterCompatibilityOptions.DuplicateObjectAsMetafile property). This technique increases the file size, but makes it possible to open this file with third-party editors which don’t support native picture format.
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 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;
}
}
}
}