Import and Export
- 5 minutes to read
ASPxHtmlEditor‘s contents can be imported and exported from/to various document formats. The supported formats include:
- Rich Text (.rtf)
- Office Open XML (.docx)
- MIME HTML (.mht)
- Open Document (.odt)
- Plain Text (.txt)
- Portable Document (.pdf). Available for export only.
Note
DevExpress controls require the DevExpress.RichEdit.v24.2.Export.dll library to export their content to DOCX or RTF format.
Import
You can only implement import operations programmatically. Use an ASPxHtmlEditor.Import method overload for this purpose. This method and its overloads allow you to import editor content from a file or stream, and specify import settings.
The overloaded ASPxHtmlEditor.Import method can include the following import parameters:
filePath
Specifies the path to a file to import its contents into ASPxHtmlEditor. This path includes the file name and extension, if any. Note that an extension type is not taken into account if you specify the method’s format parameter.
inputStream
Specifies a stream from which the editor content is to be imported.
format
Specifies the imported content format. The HtmlEditorImportFormat enumeration lists available formats. If the format parameter is not specified, the file extension defined in the filePath should match the supported formats. Otherwise, an exception occurs.
useInlineStyles
Specifies whether ASPxHtmlEditor stores document format styles (such as font settings, paragraph alignment, and so on) as in-line styles in its HTML content. If the useInlineStyles parameter is not used or it is set to
false
, the editor automatically generates a separate CSS file (within a folder specified in the contentFolder parameter) to store format styles of the imported document.contentFolder
Specifies the path to a folder to store content files of the imported document. This folder contains images used within the document content and/or an automatically created CSS file that defines content style settings (if the method’s useInlineStyles parameter is
false
or is not used). If the method’s contentFolder property is not defined, ASPxHtmlEditor places content files in the location specified in the ASPxHtmlEditor.HtmlEditorUploadControlFileSystemSettings.UploadFolder property. To access this property, use the ASPxHtmlEditor.ASPxHtmlEditorUploadSettingsBase.FileSystemSettings property.Note
Make sure that the target folder (in which ASPxHtmlEditor saves content files) is granted the required access permissions (such as the “Write” permission).
Example
The code below demonstrates how to use the following parameters of the overloaded ASPxHtmlEditor.Import method to import content from a file.
The filePath parameter
The filePath and format parameters
<dx:ASPxButton ID="btnImportFormat" runat="server" onclick="btnImportFormat_Click"
Text="Import by Format" Width="150px">
</dx:ASPxButton>
<br />
<dx:ASPxButton ID="btnImportName" runat="server" onclick="btnImportName_Click"
Text="Import by Name" Width="150px">
</dx:ASPxButton>
<br />
<dx:ASPxHtmlEditor ID="ASPxHtmlEditor1" runat="server">
</dx:ASPxHtmlEditor>
protected void btnImportFormat_Click(object sender, EventArgs e){
string filePath = @"~/Documents/SampleImportDocument1";
string contentFolder = @"~/Documents/ContentFiles";
//Imports from a DOCX file whose name does not have an extension
ASPxHtmlEditor1.Import(HtmlEditorImportFormat.Docx, filePath, false, contentFolder);
}
protected void btnImportName_Click(object sender, EventArgs e){
string filePath = @"~/Documents/SampleImportDocument2.rtf";
string contentFolder = @"~/Documents/ContentFiles";
//Imports from an RTF file whose name has an extension
ASPxHtmlEditor1.Import(filePath, true, contentFolder);
}
Export
ASPxHtmlEditor allows you to use an export toolbar item that implements export to various document formats. You can also export editor content programmatically on the client and server sides.
With a user interface
ASPxHtmlEditor allows users to export editor content with a drop-down toolbar item (ToolbarExportDropDownButton) that includes all required export functionality. Add this item to the editor’s toolbar and use its ToolbarExportDropDownButton.Items property to access and customize the collection of document formats.
Programmatically on the server side
Call an overload of the ASPxHtmlEditor.Export method to save the editor content to a stream or to the response.
The overloaded method can include the following export parameters:
fileName
Specifies the exported file’s name without an extension; the file extension will be added automatically based upon the specified format. If fileName is not defined, a default file name - the value of the control’s ID property - is used.
outputStream
Specifies a stream to which the editor content is to be exported.
format
Specifies the exported document format. Available values are listed within the HtmlEditorExportFormat enumeration.
saveAsFile
Specifies whether a client browser should save the exported document as a downloadable attachment or open it within the browser window (if the browser supports the document format).
Example
The code below demonstrates how to programmatically export ASPxHtmlEditor content to a file stream or to the response.
<dx:ASPxButton ID="btnExport" runat="server"
Text="Export" Width="150px" onclick="btnExport_Click">
</dx:ASPxButton>
<br />
<dx:ASPxButton ID="btnExportToStream" runat="server"
Text="Export to Stream" Width="150px" onclick="btnExportToStream_Click">
</dx:ASPxButton>
<br />
<dx:ASPxHtmlEditor ID="ASPxHtmlEditor1" runat="server">
</dx:ASPxHtmlEditor>
protected void btnExportToStream_Click(object sender, EventArgs e){
//Make sure that all required permissions are defined for the folder to which the exported content will be saved.
System.IO.FileStream outputStream = new System.IO.FileStream(MapPath(@"~/Documents/ExportedDocument.rtf"), System.IO.FileMode.Create);
ASPxHtmlEditor1.Export(HtmlEditorExportFormat.Rtf, outputStream);
}
protected void btnExport_Click(object sender, EventArgs e){
ASPxHtmlEditor1.Export(HtmlEditorExportFormat.Pdf, "MyFile");
}
Programmatically on the client side.
Call the ASPxClientHtmlEditor.ExecuteCommand method with the EXPORT_COMMAND commandName parameter to save the editor content to a file.
The parameter event parameter specifies the file export format. This parameter includes the following values:
- Rtf - to save content to Rich Text Format (.rtf)
- Mht - to save content to MIME HTML (.mht)
- Odt - to save content to Open Document (.odt)
- Docx - to save content to Office Open XML (.docx)
- Txt - to save content to plain text (.txt)
- Pdf - to save content to Portable Document (.pdf)
The addToUndoHistory event parameter is not in effect for the EXPORT_COMMAND command.
Example
The code below uses the client-side ExecuteCommand method to programmatically export ASPxHtmlEditor content.
<dx:ASPxHtmlEditor ID="ASPxHtmlEditor1" runat="server" ClientInstanceName="htmlEditor">
</dx:ASPxHtmlEditor>
<dx:ASPxButton ID="btnExportToPdf" runat="server" AutoPostBack="False" Text="Export to pdf">
<ClientSideEvents Click="function(s, e) {
htmlEditor.ExecuteCommand(ASPxClientCommandConsts.EXPORT_COMMAND, 'Pdf', false)
}" />
</dx:ASPxButton>
Note
Use the ASPxHtmlEditor.StylesDocument property to specify style settings for an exported document.