Export to DOCX and RTF
This help article explains how to export the Telerik ASP.NET AJAX Editor content to an MS Word document by using its built-in integration with the Telerik document processing libraries.
Since Q3 2014 the export feature of the RadEditor control supports DOCX and RTF files.
The Telerik document processing libraries support .NET Framework versions 4.0 and above.As of Q3 2014 the RadEditor export/import to RTF is not supported under .NET 3.5 .
If you are interested in the Import from MS Word functionality, navigate to the Import from DOCX and RTF article.
Quick navigation:
- Enabling the Export Feature for DOCX and RTF
- Exporting to an MS Word Document
- Defining Export Settings
- Export the File to the Server and Prevent it from Being Sent to the Client.
- Modifying the Exported Content
- Known Limitations
Enabling the Export Feature for DOCX and RTF
The built-in implementation of this feature depends on the Telerik document processing libraries.Since the Q2 2014 release, it comes with the installation of the Telerik® UI for ASP.NET AJAX suite.You can see how to obtain the needed assemblies in the Included Assemblies article.
To use the built-in document processing libraries integration with the RadEditor Export to Word feature,you need to add references to the following assemblies in your project:
- Telerik.Windows.Documents.Core.dll
- Telerik.Windows.Documents.Flow.dll
- Telerik.Windows.Zip.dll
Exporting to an MS Word Document
You can trigger an export to Word file action by calling the server-side ExportToDocx() method for DOCX ExportToRtf() method for RTF. Example 1 illustrates their use.
The default name of the exported file will be RadEditorExport.docx or RadEditorExport.rtf.Optionally, you can define it via the FileName property in the ExportSettings inner tag.You can read more about the available customization options in the Defining Export Settings and Using the Export Event sections.
You can modify the exported content when the OnExportContent event is raised.
The default export functionality requires a full postback because it has to send a file to the client and this is not possible during an AJAX request.
For correct export of HTML table elements, the PdfExportFilter content filter must be enabled
Example 1: Exporting the RadEditor content to Word documents by using two simple ASP Button controls for .docx and .rtf
<telerik:RadEditor RenderMode="Lightweight" runat="server" ID="RadEditor1" ContentFilters="PdfExportFilter, DefaultFilters">
<Content>
<h1>Heading</h1>
<p>Some content</p>
</Content>
</telerik:RadEditor>
<asp:Button Text="Export to RTF" runat="server" ID="ExportRTF" OnClick="ExportRtf_Click" />
<asp:Button Text="Export to DOCX" runat="server" ID="ExportDOCX" OnClick="ExportDocx_Click"/>
Defining Export Settings
Optionally, you can configure the export settings via the Docx (for DOCX documents)or Rtf (for RTF documents) inner tag, located in the ExportSettings inner tag of the RadEditor control.
The available options are:
- DefaultFontName—sets the default font name for the Word document.
- DefaultFontSizeInPoints—sets the default font size for the Word document.
- PageHeader—defines a header text for the exported document.
- HeaderFontSizeInPoints—sets the font size for the header element.
Example 2: Using the ExportSettings options of the DOCX and RTF exporting functionality
<telerik:RadEditor RenderMode="Lightweight" runat="server" ID="RadEditor1" ContentFilters="PdfExportFilter, DefaultFilters">
<ExportSettings>
<Docx DefaultFontName="Arial" DefaultFontSizeInPoints="12" HeaderFontSizeInPoints="8" PageHeader="Some header text for DOCX documents" />
<Rtf DefaultFontName="Times New Roman" DefaultFontSizeInPoints="13" HeaderFontSizeInPoints="9" PageHeader="Some header text for RTF documents" />
</ExportSettings>
<Content>
<h1>Heading</h1>
<p>Some content</p>
</Content>
</telerik:RadEditor>
Modifying the Exported Content
Exporting content from the Editor raises the ExportContent event that can be handled to accommodate the concrete application requirements. Additional information about the event arguments and how they can be used to interact with the content is available in the OnExportContent article.
Example 3: Dynamically adding header and footer elements to the exported document via the ExportContent event
<telerik:RadEditor RenderMode="Lightweight" runat="server" ID="RadEditor1" OnExportContent="RadEditor1_ExportContent" ContentFilters="DefaultFilters, PdfExportFilter"></telerik:RadEditor>
<asp:Button runat="server" ID="Button1" Text="Export to DOCX" OnClick="Button1_Click" />
Known Limitations
- Lists cannot be exported.
- The CSS page-break-after property is not exported.
- Image borders are not exported.
- Image dimensions are not exported. Exported images will be in their original size regardless of custom dimensions being set in the HTML.
- Images with URL source cannot be exported. This is fixed in Q2 2016. For earlier versions, a workaround is available at https://feedback.telerik.com/Project/108/Feedback/Details/168922.