Back to Devexpress

Import and Export

aspnet-9859-components-html-editor-concepts-import-and-export.md

latest6.4 KB
Original Source

Import and Export

  • Jun 16, 2022
  • 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.v25.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

  • inputStream

  • format

  • useInlineStyles

  • contentFolder

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

  • ASPX

aspx
<dx:ASPxButton ID="btnImportFormat" runat="server" onclick="btnImportFormat_Click" 
        Text="Import by Format" Width="150px">
    </dx:ASPxButton>
    


    <dx:ASPxButton ID="btnImportName" runat="server" onclick="btnImportName_Click" 
        Text="Import by Name" Width="150px">
    </dx:ASPxButton>
    


    <dx:ASPxHtmlEditor ID="ASPxHtmlEditor1" runat="server">
    </dx:ASPxHtmlEditor>
csharp
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

  • outputStream

  • format

  • saveAsFile

Example

The code below demonstrates how to programmatically export ASPxHtmlEditor content to a file stream or to the response.

aspx
<dx:ASPxButton ID="btnExport" runat="server" 
        Text="Export" Width="150px" onclick="btnExport_Click">
    </dx:ASPxButton>
    


    <dx:ASPxButton ID="btnExportToStream" runat="server" 
        Text="Export to Stream" Width="150px" onclick="btnExportToStream_Click">
    </dx:ASPxButton>
    


    <dx:ASPxHtmlEditor ID="ASPxHtmlEditor1" runat="server">
    </dx:ASPxHtmlEditor>
csharp
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.

aspx
<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.