Skip to main content
A newer version of this page is available. .

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.

Import

Import operations can only be initiated programmatically. Use a ASPxHtmlEditor.Import method overload for this purpose. With this method, you can import editor content from a file or stream and specify some additional settings.

The main import settings (available via the method’s parameters) include:

  • filePath

    Specifies the path to a file (including the file name and extension, if any) whose content is to be imported into ASPxHtmlEditor. The file extension type is not taken into account if the import format is specified via format.

  • inputStream

    Specifies a stream from which the editor content is to be imported.

  • format

    Specifies the imported content format. Available values are listed within the HtmlEditorImportFormat enumeration. If the format is not specified, a file defined via filePath should have an extension matching supported formats; otherwise an exception occurs.

  • useInlineStyles

    Specifies whether or not document formatting styles (such as font settings, paragraph alignments, etc.) should be embedded into the editor’s HTML content as in-line styles. If this parameter is not used or it is set to false, a specific separate css file will be automatically generated within a folder specified by the contentFolder parameter to store formatting styles used in the imported document.

  • contentFolder

    Specifies the path to a folder that should contain files required to represent the imported document’s content. This folder is intended to contain images used within the document content and/or (if useInlineStyles is false or is not used) an automatically created css file that defines content style settings. If the contentFolder is not explicitly defined, content files will be placed in a default location specified by the ASPxHtmlEditorImageUploadSettings.UploadImageFolder property available via the editor’s ASPxHtmlEditor.SettingsImageUpload property.

    Note

    Make sure that a target folder (to which content files will be saved) is granted the required access permissions (such as ‘Write’).

Example

The code below illustrates how to import the contents from a file, specifying either the file path and import format, or just a file path.

    <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 a RTF file whose name has an extension
        ASPxHtmlEditor1.Import(filePath, true, contentFolder);
    }

Export

Export of ASPxHtmlEditor contents can be initiated in the following manner.

Via a user interface.

You can allow end-users to export editor content via a specially designed toolbar item (ToolbarExportDropDownButton) that provides all the required functionality out of the box. All you have to do is add it to the editor’s toolbar and customize output document formats available for end-users via the button’s ToolbarExportDropDownButton.Items collection.

ExportButton.png

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 main export settings (available via the method’s parameters) include:

  • 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 document format is allowed to be opening).

Example

This sample code demonstrates how ASPxHtmlEditor’s contents can be programmatically exported, either 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 a 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.

To save the editor contents to a file, call the ASPxClientHtmlEditor.ExecuteCommand method with the EXPORT_COMMAND commandName parameter.

The parameter event setting specifies the format to export file. The available setting values are listed below.

  • Rtf - to save a content to Rich Text Format (.rtf)
  • Mht - to save a content to MIME HTML (.mht)
  • Odt - to save a content to Open Document (.odt)
  • Docx - to save a content to Office Open XML (.docx)
  • Txt - to save a content to Plain Text (.txt)
  • Pdf - to save a content to Portable Document (.pdf)

The addToUndoHistory event parameter is not in effect for the EXPORT_COMMAND command.

Example

This code sample demonstrates how ASPxHtmlEditor’s contents can be programmatically exported using the client-side ExecuteCommand method.

<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

You can specify style settings that will be applied to an exported document by using the ASPxHtmlEditor.StylesDocument property.