Import and Export
- 4 minutes to read
This document contains an overview of techniques, methods and properties used to load and save documents in different formats.
#Load a Document
To load a file into the DXRichEdit control, you can call a LoadDocumentCommand that invokes a standard end-user dialog. RichEdit API provides two methods - RichEditControl.LoadDocument and Document.LoadDocument - 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 a document, SubDocument.InsertDocumentContent can be used. It enables you to perform insertion from a file or a stream, and to 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.
To insert a picture in a document, use the InsertPictureCommand command or the SubDocument.InsertImage method.
#Save a Document
To save a document, call the SaveDocumentAsCommand command that invokes the Save As... dialog. The RichEditBehaviorOptions.SaveAs and RichEditBehaviorOptions.Save properties allow you to restrict the capability of saving files for end-users.
RichEditControl provides you with the RichEditControl.SaveDocument and Document.SaveDocument methods, allowing you to export a document to any of the supported file formats.
To get images contained within a specified document range, use the SubDocument.GetImages method. Then you can use the OfficeImage.GetImageBytesSafe method to obtain a stream of image data, which can be saved to a file or to another destination.
Each format has its own specifics and XtraRichEdit allows you to specify options that affect the output file. Some of them are listed below.
#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.
#Rich Text Format
The RichEditControl.RtfText method returns formatted document contents. SubDocument.GetRtfText gets formatted text of a specified range.
When a document is saved in an RTF format, inline pictures are saved two times by default - in the native format and as a metafile. Use the RtfDocumentExporterCompatibilityOptions.DuplicateObjectAsMetafile property to modify this behavior.
#HTML format
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 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.
#MHT Format
The SubDocument.GetMhtText method gets the contents of a specified range in a MHT format.
#MS Word 2007 Format (OpenXml)
The SubDocument.GetOpenXmlBytes method gets contents of a specified range in an OpenXml format.
#WordML Format
The SubDocument.GetWordMLText method gets contents of a specified range in a WordML format.
#DOC (MS Word 97-2003) Format
Use RichEditControl.LoadDocument and RichEditControl.SaveDocument methods to import and export documents in this format.