This document describes how to load and save documents in different formats. It consists of the following sections:

Built-in UI
End users can click the Open button on the File ribbon tab to invoke the Open dialog. Select the file you want to open and click Open.

The Save and Save As buttons on the File ribbon tab allow users to save the changes made to the current document or save it as a new file. Click the Save As button to invoke the Save As dialog used to save a copy as a new file, specify the document's location, name, and format. Refer to the Lesson 1 - Create a Simple Rich Text Editor for details on how to provide a ribbon UI for the RichEditControl.

The DXRichEditDocumentSaveOptions.CurrentFileName property returns the current document's file name (including the path and extension).

Initiate Load and Save Operations in Code
XAML
Use the RichEditControl.DocumentSource property to bind the RichEditControl to a document source from XAML. The following data sources are supported:
- System.IO.Stream;
- System.Uri;
- System.String (document content or document path);
- System.IO.FileInfo;
- System.Byte[];
- RichEditDocumentSource.
You can use the DXBinding extension to call a method that returns a valid document source directly in XAML, as in the following code:
XAML |
<dxre:RichEditControl Name="richEditControl1"
CommandBarStyle="Ribbon"
DocumentSource="{DXBinding $local:SourceHelper.GetDocumentSource()'}">
</dxre:RichEditControl>
|
Changes made in the bound document are not saved automatically. Refer to the Lesson 7 - Bind the RichEditControl to a Document Source using the MVVM pattern topic for information on how to implement a custom command to save changes to the database.
Commands
Execute one of the following commands to load or save a document:
API
The RichEditControl provides the following methods to load or save the document and specify its options. Refer to the Files examples section for code samples.

How to: Load and Save a Document
The code sample below loads the document from a file stream when the form is opened and saves the result to the file when the form is closed. Refer to the How to: Load a Document and How to: Save a Document examples for more code samples.
C# |
private void Form1_Load(object sender, EventArgs e)
{
using (Stream stream = new FileStream("FirstLook.docx", FileMode.Open))
{
richEditControl.LoadDocument(stream);
}
}
...
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
richEditControl.SaveDocument("Result.docx", DocumentFormat.OpenXml);
}
|
VB |
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
Using stream As Stream = New FileStream("FirstLook.docx", FileMode.Open)
richEditControl.LoadDocument(stream)
End Using
End Sub
...
Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
richEditControl.SaveDocument("Result.docx", DocumentFormat.OpenXml)
End Sub
|

Basic Format-Specific API
The table below lists document formats the RichEditControl supports, and the API used to set format-specific import and export options. You can specify these options in the RichEditControl.BeforeImport or RichEditControl.BeforeExport event handlers.

How to: Handle BeforeImport and BeforeExport events
Tip
Check Export and Files examples section for more information on how to accomplish an import- or export-related task.
How to: Handle the BeforeImport Event
The code sample below illustrates how to handle RichEditControl.BeforeImport event for different document formats:
C# |
private void RichEditControl_BeforeImport(object sender, BeforeImportEventArgs e)
{
if (e.DocumentFormat == DocumentFormat.PlainText)
{
((DXRichEditPlainTextDocumentImporterOptions)e.Options).AutoDetectEncoding = true;
}
if (e.DocumentFormat == DocumentFormat.Doc)
{
((DXRichEditDocDocumentImporterOptions)e.Options).KeepCommentsForRemovedRanges = false;
}
if (e.DocumentFormat == DocumentFormat.Html)
{
((DXRichEditHtmlDocumentImporterOptions)e.Options).AsyncImageLoading = false;
}
}
|
VB |
Private Sub RichEditControl_BeforeImport(ByVal sender As Object, ByVal e As BeforeImportEventArgs)
If e.DocumentFormat = DocumentFormat.PlainText Then
(CType(e.Options, DXRichEditPlainTextDocumentImporterOptions)).AutoDetectEncoding = True
End If
If e.DocumentFormat = DocumentFormat.Doc Then
(CType(e.Options, DXRichEditDocDocumentImporterOptions)).KeepCommentsForRemovedRanges = False
End If
If e.DocumentFormat = DocumentFormat.Html Then
(CType(e.Options, DXRichEditHtmlDocumentImporterOptions)).AsyncImageLoading = False
End If
End Sub
|
How to: Handle the BeforeExport Event
The code sample below shows how specify export options for different formats in the RichEditControl.BeforeExport event handler.
C# |
private void RichEditControl_BeforeExport(object sender, BeforeExportEventArgs e)
{
if (e.DocumentFormat == DocumentFormat.PlainText)
{
DXRichEditPlainTextDocumentExporterOptions plainTextOptions = e.Options as DXRichEditPlainTextDocumentExporterOptions;
plainTextOptions.ExportHiddenText = true;
plainTextOptions.FieldCodeEndMarker = ">";
plainTextOptions.FieldCodeStartMarker = "[<";
plainTextOptions.FieldResultEndMarker = "]";
}
if (e.DocumentFormat == DocumentFormat.OpenXml)
{
DXRichEditOpenXmlDocumentExporterOptions docxOptions = e.Options as DXRichEditOpenXmlDocumentExporterOptions;
docxOptions.ExportedDocumentProperties = DocumentPropertyNames.Title | DocumentPropertyNames.LastModifiedBy | DocumentPropertyNames.Modified;
}
if (e.DocumentFormat == DocumentFormat.Html)
{
DXRichEditHtmlDocumentExporterOptions htmlOptions = e.Options as DXRichEditHtmlDocumentExporterOptions;
htmlOptions.EmbedImages = true;
htmlOptions.CssPropertiesExportType = CssPropertiesExportType.Style;
htmlOptions.UseFontSubstitution = false;
}
}
|
VB |
Private Sub RichEditControl_BeforeExport(ByVal sender As Object, ByVal e As BeforeExportEventArgs)
If e.DocumentFormat = DocumentFormat.PlainText Then
Dim plainTextOptions As DXRichEditPlainTextDocumentExporterOptions = TryCast(e.Options, DXRichEditPlainTextDocumentExporterOptions)
plainTextOptions.ExportHiddenText = True
plainTextOptions.FieldCodeEndMarker = ">"
plainTextOptions.FieldCodeStartMarker = "[<"
plainTextOptions.FieldResultEndMarker = "]"
End If
If e.DocumentFormat = DocumentFormat.OpenXml Then
Dim docxOptions As DXRichEditOpenXmlDocumentExporterOptions = TryCast(e.Options, DXRichEditOpenXmlDocumentExporterOptions)
docxOptions.ExportedDocumentProperties = DocumentPropertyNames.Title Or DocumentPropertyNames.LastModifiedBy Or DocumentPropertyNames.Modified
End If
If e.DocumentFormat = DocumentFormat.Html Then
Dim htmlOptions As DXRichEditHtmlDocumentExporterOptions = TryCast(e.Options, DXRichEditHtmlDocumentExporterOptions)
htmlOptions.EmbedImages = True
htmlOptions.CssPropertiesExportType = CssPropertiesExportType.Style
htmlOptions.UseFontSubstitution = False
End If
End Sub
|

Notes on Document Content
Control Document Elements on Load
Use the RichEditControl.DocumentCapabilitiesOptions property to access the DXRichEditDocumentCapabilitiesOptions properties to control what document elements to import. Specify the options in the RichEditControl.BeforeImport event handler, as shown below. The restricted elements are lost when the document is saved.
C# |
private void Server_BeforeImport(object sender, BeforeImportEventArgs e)
{
server.Options.DocumentCapabilities.CharacterFormatting = DocumentCapability.Disabled;
server.Options.DocumentCapabilities.Comments = DocumentCapability.Disabled;
server.Options.DocumentCapabilities.InlineShapes = DocumentCapability.Disabled;
}
|
VB |
Private Sub Server_BeforeImport(ByVal sender As Object, ByVal e As BeforeImportEventArgs)
server.Options.DocumentCapabilities.CharacterFormatting = DocumentCapability.Disabled
server.Options.DocumentCapabilities.Comments = DocumentCapability.Disabled
server.Options.DocumentCapabilities.InlineShapes = DocumentCapability.Disabled
End Sub
|
HTML Tag Support
The RichEditControl parses and transforms loaded HTML documents into the internal document model. However, not every HTML tag can be converted into a corresponding document model element. Refer to the HTML Tag Support topic for a list of supported tags.

See Also