Telerik’s RadRichTextBox control allows you to display rich text content in your Silverlight application. It can hold formatted text, images, tables, hyperlinks etc. to provide a visual look like Microsoft Word document. To use a simple RadRichTextBox control, you will need reference to the following Telerik assemblies: Telerik.Windows.Controls, Telerik.Windows.Data and Telerik.Windows.Documents in your WPF/Silverlight project.
The control library not only allows you to display rich text content, but also helps you to export the same to different file formats so that you can save it to file or database. I was working on an application which has the Microsoft Word kind functionality and recently the client asked to add functionality to export the content to HTML, PDF, DOCX format in addition to save it in the database as an image. Telerik library helped me a lot to implement the same in that application with just a few lines of code. The library already exposed different APIs to handle different file formats in order to export the content. In this article I will cover how the Telerik’s RadRichTextBox helped me exporting the rich text content added by user to a file of various formats like HTML, PDF, RTF, DOCX, XAML and TXT.
Telerik’s Rad Library provides a different Format Provider class for each category, which inherits IDocumentFormatProvider interface present under Telerik.Windows.Documents.FormatProviders namespace. This interface provides various APIs in order to do import and export operations of a RadDocument. Here is the meta data of the interface for you to understand the basics:
Each format provider (except TXT Format Provider) class is available in a separate assembly matching the name of the provider. Hence if you want to use only PDF provider, you just have to add the PDF format provider assembly in your project. Here is a list of providers and their respective assembly name:
So now let us see how this providers will help you exporting the rich content. Keep in mind that, it can only import or export a no. of features/controls supported by it. Other controls like TextBox, CheckBox etc. will be stripped off while importing a document created by an advanced editor like Microsoft Word.
Exporting a document is easy enough. If you are using RadRibbonButton, you just have to bind proper RichTextCommand with it’s respected CommandParameter as shown below:
Here for all types of provider, the RichTextCommand binding will be with SaveCommand and the CommandParameter (in this case, EXPORT_TYPE) will have different values like html, pdf, rtf, docx, xaml, txt based on the format type that you want to export the content of the RadRichTextBox. For example, telerik:CommandParameter=”html” will export the content to HTML file format.
In case you are not using command binding (RichTextCommand), you can write code explicitly on button click to get the stream of the file where you want to save it. Then create an instance of the appropriate provider and call the Export(…) method passing the document object and the stream. Here is an example of exporting rich content of RadRichTextBox to a stream:
To export a RadRichTextBox document to different file formats, you can use this simple method:
In order to invoke the method to export the content, pass proper value to the method
parameters (i.e. defaultExtension, filter and formatProvider). Here is an example which shows you how to call the said method with different parameters to export the rich content to different file format:
Not only this, you can also export a rad document to a byte array by passing the instance of the rad Document to the export method. Let’s see an example to make the things clearer. Here we will see sample code to export the rich text document to various document formats.
The above screenshot is the UI of our demo application (source code attached at the bottom of the article), where we have two button sets. The first set is the ribbon buttons (top) which uses the command binding to implement the export feature and the second set is the default buttons (right) where I demonstrated how to export the rich content to different types by explicitly writing code.
In general it is very easy when you do a proper binding in the XAML page. This reduces the lines of extra code in your code behind. Alternatively, you can also execute the command from code behind in order to stop writing the code from scratch.
I hope that the article was easy to understand and this will help you to integrate the export functionality of RadRichTextBox control in your WPF application. Though the above demo was done in WPF, but this will also work fine for Silverlight application. You can grab the source code from here.
Kunal Chowdhury - Microsoft MVP (Silverlight) | Telerik MVP & Insider
My Blog | Twitter | Facebook | Silverlight-Zone
Subscribe to be the first to get our expert-written articles and tutorials for developers!