Log In
[Expand]General Information
[Collapse]WinForms Controls
 [Expand]What's Installed
 [Expand]Build an Application
 [Collapse]Controls and Libraries
  [Expand]Forms and User Controls
  [Expand]Messages, Notifications and Dialogs
  [Expand]Editors and Simple Controls
  [Expand]Ribbon, Bars and Menu
  [Expand]Application UI Manager
  [Expand]Docking Library
  [Expand]Data Grid
  [Expand]Vertical Grid
  [Expand]Pivot Grid
  [Expand]Tree List
  [Expand]Chart Control
  [Expand]Map Control
  [Collapse]Rich Text Editor
   [Expand]Product Information
    Product Class Structure
   [Expand]Getting Started
   [Expand]RichEditControl Document
    Text Formatting
    Import and Export
   [Expand]Page Layout
    Spell Checking
    Mail Merge
    Restrictions and Protection
    Syntax Highlighting
    Traversing the Document
   [Expand]Visual Elements
   [Expand]Member Tables
    Keyboard Shortcuts
    HTML Tag Interpretation
  [Expand]Spell Checker
  [Expand]Form Layout Managers
  [Expand]Navigation Controls
  [Expand]PDF Viewer
  [Expand]TreeMap Control
 [Expand]Common Features
  Get More Help
 [Expand]API Reference
[Expand]ASP.NET Controls and MVC Extensions
[Expand]ASP.NET Bootstrap Controls
[Expand]ASP.NET Core Bootstrap Controls
[Expand]WPF Controls
[Expand]Xamarin Controls
[Expand]Windows 10 App Controls
[Expand]Document Server
[Expand]Report Server
[Expand]eXpressApp Framework
[Expand]CodeRush Classic
[Expand]Cross-Platform Core Libraries
[Expand]Tools and Utilities
 End-User Documentation

Import and Export

This document contains an overview of techniques, methods and properties used to load and save documents in different formats.

Expanded Load a Document

To load a file into the XtraRichEdit control, you can call a LoadDocumentCommand that invokes a standard end-user dialog. RichEdit API provides two methods - RichEditControl.LoadDocument and Document.LoadDocument. They can be used to load a document from a file or a stream; a file format can be specified explicitly or deduced from the file extension.

By specifying the RichEditBehaviorOptions.Open property value, LoadDocumentCommand can be hidden or disabled, so that end-users cannot load documents.

To import a part of the document, SubDocument.InsertDocumentContent can be used. It enables you to perform insertion from a file or a stream and specify the format of the inserted document. The SubDocument.AppendDocumentContent method imports a document and inserts its content to the end of the current document.

Expanded Save a Document

To save a document, call the SaveDocumentAsCommand command that invokes the Save As... dialog. You can also use SaveDocumentCommand to save modifications under the same name. The RichEditBehaviorOptions.SaveAs and RichEditBehaviorOptions.Save properties allow you to restrict the capability to save files for end-users.

RichEdit API has the RichEditControl.SaveDocument and Document.SaveDocument methods, allowing you to export a document to any of the supported file formats.

Each format has its own specifics and XtraRichEdit allows you to specify options that affect the output file. Some of them are listed below.

Expanded Plain Text

The RichEditControl.Text method enables you to get document contents as plain text. The SubDocument.GetText method obtains a plain text of the specified range. If a text marked as hidden needs to be included in the plain text output, set the PlainTextDocumentExporterOptions.ExportHiddenText property to true (it is false by default).

Use the PlainTextDocumentImporterOptions.Encoding property to specify character encoding for a proper conversion of an imported text or leave the PlainTextDocumentImporterOptions.AutoDetectEncoding option with its default true value, so the control can determine the encoding for itself.

You can include field codes in the resulting text. To accomplish this, set the ExportHiddenText option to true and specify field code/result markers via the PlainTextDocumentExporterOptions.FieldCodeStartMarker, PlainTextDocumentExporterOptions.FieldCodeEndMarker, PlainTextDocumentExporterOptions.FieldResultEndMarker properties.

Expanded Rich Text Format

The RichEditControl.RtfText method returns a formatted document's contents. SubDocument.GetRtfText gets formatted text of a specified range.

When a document is saved in an RTF format, inline pictures can be stored twice - in the native format and as Windows Metafile. If the editing application is unable to process the native picture format, it can display a Windows Metafile. By default only picture in native format is saved, to reduce the file size. Use the RtfDocumentExporterCompatibilityOptions.DuplicateObjectAsMetafile property to modify this behavior.

All document styles are saved by default, regardless of whether they are actually used. It may unreasonably increase the size of the saved document. You can delete unused styles from Document.CharacterStyles, Document.ParagraphStyles and Document.TableStyles style collections before export to reduce the size of data in RTF format.

Expanded HTML format

Rich Text Editor does not operate with HTML data directly. Instead, HTML document is parsed and transformed into internal document model. Therefore, not every HTML tag can be converted into the corresponding document model element. When exporting a document to HTML, the conversion process runs again, resulting in a document that is different from the original HTML document.

Supported HTML tags are listed in the HTML Tag Interpretation article.

The SubDocument.GetHtmlText method obtains HTML formatted contents of a specified range.

When an HTML document is loaded, you can implement your own IUriStreamProvider or the IUriStreamService descendants and override the IUriStreamProvider.GetStream method to gain full control over decoding external contents in an HTML file. When a RichEditControl needs to load data from a specific URI, your method will be executed instead of the default.

When a document is exported to an HTML format, you can specify a location for storing images as external files or process them before saving. To accomplish this, handle the RichEditControl.BeforeExport event and specify the HtmlDocumentExporterOptions.UriExportType and IExporterOptions.TargetUri. Moreover, you can implement your own class with the IUriProvider interface and override the IUriProvider.CreateImageUri and IUriProvider.CreateCssUri methods. Then, it can be used instead of the default URI provider when the SubDocument.GetHtmlText method is called.

Another option lets you store images in HTML in base-64 encoding. The HtmlDocumentExporterOptions.EmbedImages property provides this functionality.

To substitute Tab (0x09) characters with any symbol combination in HTML output, use the HtmlDocumentExporterOptions.TabMarker property.

To mark the text in HTML output with H1 - H6 tags, specify the corresponding Paragraph.OutlineLevel of the paragraph. Paragraphs with outline levels higher than 6 are exported as text enclosed in P tag.

Expanded MHT Format

The SubDocument.GetMhtText method gets the contents of a specified range in a MHT format.

Expanded MS Word 2007 Format (OpenXml)

The SubDocument.GetOpenXmlBytes method gets contents of a specified range in an OpenXml format.

Expanded WordML Format

The SubDocument.GetWordMLText method gets contents of a specified range in a WordML format.

Expanded DOC (MS Word 97-2003) Format

Use RichEditControl.LoadDocument and RichEditControl.SaveDocument methods to import and export documents in this format.

Expanded PDF

Use the RichEditControl.ExportToPdf method to export document in PDF. Currently you cannot load and edit PDF document using XtraRichEdit.

How would you rate this topic?​​​​​​​