Import and Export in Rich Text Editor for WPF
- 7 minutes to read
This document describes how to load and save documents in different formats.
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 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).
Tip
You can prohibit end-users from creating, loading or saving documents. Set the DXRichEditBehaviorOptions.CreateNew or DXRichEditBehaviorOptions.Open, DXRichEditBehaviorOptions.Save or DXRichEditBehaviorOptions.SaveAs property to DocumentCapability.Disabled to disable or hide the corresponding commands in the Ribbon UI and pop-up menu.
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:
You can use the DXBinding extension to call a method that returns a valid document source directly in XAML, as in the following code:
<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:
LoadDocumentCommand - invokes the Open dialog and loads the selected document.
SaveDocumentCommand - saves the changes made to the document. If the document is saved for the first time, it invokes the Save As dialog. Otherwise, this command saves the document with the name and format the DXRichEditDocumentSaveOptions.CurrentFileName and DXRichEditDocumentSaveOptions.CurrentFormat properties specify.
SaveDocumentAsCommand - always invokes the Save As dialog and saves the document as a new file with the specified name and format.
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.
Member | Description |
---|---|
RichEditControl.LoadDocument | Loads a document from a file or stream. The document format can be specified by one of the DocumentFormat enumeration values or detected automatically. |
RichEditControl.LoadDocumentTemplate | Loads a document template from the specified file or stream so that it cannot be overwritten automatically. |
Document.LoadDocument | Loads a document from the specified file or stream. Its content can be specified by one of the DocumentFormat enumeration values or is automatically determined based on the file’s format. |
SubDocument.InsertDocumentContent | Inserts content from the selected range into the current document at the specified position. |
SubDocument.AppendDocumentContent | Appends content from the selected range. |
RichEditControl.SaveDocument | Saves the control’s document to a file or stream and specifies the document’s format. |
RichEditControl.SaveDocumentAs | Invokes the Save As dialog and saves the document. |
Document.SaveDocument | Saves the document to a file or stream and specifies the document’s format. |
Note
The SaveDocumentCommand or SaveDocumentAsCommand execution sets the RichEditControl.Modified property to false. The RichEditControl.SaveDocument, RichEditControl.SaveDocumentAs or Document.SaveDocument method call does not automatically change the Modified property value.
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.
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);
}
Note
We do not recommend that you use the DocumentFormat.Undefined field as the SaveDocument
method parameter. Otherwise, the document is saved with invalid format.
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.
Tip
Use the SubDocument‘s Get… methods to retrieve and format a part of the document. Refer to the Positions and Ranges topic for details.
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:
private void RichEditControl_BeforeImport(object sender, BeforeImportEventArgs e)
{
if (e.DocumentFormat == DocumentFormat.PlainText)
{
//Detect plain text encoding automatically:
((DXRichEditPlainTextDocumentImporterOptions)e.Options).AutoDetectEncoding = true;
}
if (e.DocumentFormat == DocumentFormat.Doc)
{
//Disable loading comments added to removed ranges in DOC documents
((DXRichEditDocDocumentImporterOptions)e.Options).KeepCommentsForRemovedRanges = false;
}
if (e.DocumentFormat == DocumentFormat.Html)
{
//Load images synchronously with HTML documents
((DXRichEditHtmlDocumentImporterOptions)e.Options).AsyncImageLoading = false;
}
}
How to: Handle the BeforeExport Event
The code sample below shows how specify export options for different formats in the RichEditControl.BeforeExport event handler.
private void RichEditControl_BeforeExport(object sender, BeforeExportEventArgs e)
{
if (e.DocumentFormat == DocumentFormat.PlainText)
{
//Include document fields in the exported plain text:
DXRichEditPlainTextDocumentExporterOptions plainTextOptions = e.Options as DXRichEditPlainTextDocumentExporterOptions;
plainTextOptions.ExportHiddenText = true;
plainTextOptions.FieldCodeEndMarker = ">";
plainTextOptions.FieldCodeStartMarker = "[<";
plainTextOptions.FieldResultEndMarker = "]";
}
if (e.DocumentFormat == DocumentFormat.OpenXml)
{
//Specify what DOCX document properties to export:
DXRichEditOpenXmlDocumentExporterOptions docxOptions = e.Options as DXRichEditOpenXmlDocumentExporterOptions;
docxOptions.ExportedDocumentProperties = DocumentPropertyNames.Title | DocumentPropertyNames.LastModifiedBy | DocumentPropertyNames.Modified;
}
if (e.DocumentFormat == DocumentFormat.Html)
{
//Specify HTML export options:
DXRichEditHtmlDocumentExporterOptions htmlOptions = e.Options as DXRichEditHtmlDocumentExporterOptions;
htmlOptions.EmbedImages = true;
htmlOptions.CssPropertiesExportType = CssPropertiesExportType.Style;
htmlOptions.UseFontSubstitution = false;
}
}
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.
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;
}
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.