Earlier this year I wrote a blog about how the PDF export functionality of Telerik’s ASP.NET rich text Editor can be easily improved by using the PDF document provider implemented in the RadContols for WPF suite. Since we recently received questions about some missing features in the editor’s RTF support, I decided to implement a solution extending the default functionality. It is similar to the one explained in the previous article, because it leverages the HTML and RTF document providers contained in the same XAML suite.
You can easily get the additional functionality provided in the XAML document providers by just creating a class, which implements the RadEditorExportTemplate abstract class defined in the Telerik.Web.UI.Editor.Export namespace of the Telerik.Web.UI assembly. Then you need to use the SetRtfExportTemplate RadEditor method to inject an instance of the created class in the editor’s export to RTF functionality.
Let’s start with an ASP.NET Empty Web Application for .NET Framework 4.5 project named RadEditorRtfExportEnhancement
Then I will add references to the latest 2013 Q2 Telerik assemblies, which I need to use. These are the Telerik.Web.UI from RadControls for ASP.NET AJAX and Telerik.Windows.Controls, Telerik.Windows.Data, Telerik.Windows.Documents, Telerik.Windows.Documents.FormatProviders.Html, Telerik.Windows.Documents.FormatProviders.Rtf, Telerik.Windows.Zip assemblies, which are part of our WPF suite.
Next I will add a Web Form page named Default.aspx, containing the following code:
Markup<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RadEditorRtfExportEnhancement.Default" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!DOCTYPE html>
<
html
xmlns
=
"http://www.w3.org/1999/xhtml"
>
<
head
runat
=
"server"
>
<
title
>RadEditor Rtf Export Enhancement</
title
>
</
head
>
<
body
>
<
form
id
=
"form1"
runat
=
"server"
>
<
asp:ScriptManager
runat
=
"server"
ID
=
"ScriptManager1"
/>
<
telerik:RadEditor
ID
=
"RadEditor1"
runat
=
"server"
>
<
ExportSettings
FileName
=
"RadEditorExport"
OpenInNewWindow
=
"true"
></
ExportSettings
>
</
telerik:RadEditor
>
<
asp:Button
Text
=
"export to RTF"
runat
=
"server"
ID
=
"BtnExportToRtf"
OnClick
=
"BtnExportToRtf_Click"
/>
</
form
>
</
body
>
The code behind looks like this:
C#using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
namespace
RadEditorRtfExportEnhancement
{
public
partial
class
Default : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
RadEditor1.SetRtfExportTemplate(
new
TelerikRtfExportTemplate(RadEditor1));
}
protected
void
BtnExportToRtf_Click(
object
sender, EventArgs e)
{
RadEditor1.ExportToRtf();
}
}
}
As you can see, RadEditor's content is exported in the BtnExportToRtf_Click handler and in the Page_Load method the editor is configured to use the custom TelerikRtfExportTemplate export template class. This is done by the SetRtfExportTemplate method.
The only thing left to be done is the implementation of the mentioned above class. More information about the export template design can be found in my previous post Using an external library for the export to PDF functionality in Telerik’s ASP.NET Editor and in the "Using an external HTML to PDF conversion library" section of the following online help article Export to PDF.
Here is the source code of the class:using
System;
using
System.IO;
using
System.Reflection;
using
System.Threading;
using
System.Xml;
using
System.Xml.Xsl;
using
Telerik.Web.UI;
using
Telerik.Web.UI.Editor.Export;
using
Telerik.Windows.Documents.FormatProviders.Html;
using
Telerik.Windows.Documents.FormatProviders.Rtf;
using
Telerik.Windows.Documents.Model;
namespace
RadEditorRtfExportEnhancement
{
class
TelerikRtfExportTemplate: RadEditorExportTemplate
{
public
TelerikRtfExportTemplate(RadEditor radEditor)
:
base
(radEditor)
{
}
//no need to initialize the XmlContent
protected
override
void
InitializeXmlContent()
{
}
protected
override
string
GenerateOutput()
{
string
output =
""
;
var thread =
new
Thread(() =>
{
RtfFormatProvider provider =
new
RtfFormatProvider();
RadDocument document =
new
HtmlFormatProvider().Import(editor.Content);
string
a = provider.Export(document);
output = a;
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return
output;
}
protected
override
string
ContentType
{
get
{
return
"application/rtf"
; }
}
protected
override
string
FileExtension
{
get
{
return
".rtf"
; }
}
protected
override
ExportType ExportType
{
get
{
return
ExportType.Rtf; }
}
}
}
By creating a simple example I showed how an external tool can be integrated in the RadEditor’s export to RTF functionality. In this case it was a library created by Telerik, so owning more than one of the Telerik’s controls suites or even the DevCraft bundle is totally worth it. You can download the source code of this sample here and use it directly in your projects.
Now if you experience any problems with RadEditor’s default RTF support, we encourage you to take advantage of the provided solution. Feel free to comment and even share other cases when different RadControls suites can be used in collaboration.
Stanimir Patarinski is currently a senior software developer at Telerik Corp. He joined the company in the end of 2008 and his experience is centered on the ASP.NET AJAX products from Telerik, particularly the RadEditor control. He is an MCPD certified Web, SharePoint and Azure developer and has extensive knowledge about the integration of RadControls in third-party applications including SharePoint 2007, 2010, 2013.