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.

Introduction to Format Providers

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:

namespace Telerik.Windows.Documents.FormatProviders
{
    public interface IDocumentFormatProvider
    {
        string Name { get; }
        string FilesDescription { get; }
          
        IEnumerable<string> SupportedExtensions { get; }
          
        bool CanImport { get; }
        bool CanExport { get; }
          
        RadDocument Import(Stream input);
        RadDocument Import(byte[] input);
          
        void Export(RadDocument document, Stream output);
        byte[] Export(RadDocument document);
    }
}
  

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:

  • HTMLTelerik.Windows.Documents.FormatProviders.Html.dll
  • PDFTelerik.Windows.Documents.FormatProviders.Pdf.dll
  • RTFTelerik.Windows.Documents.FormatProviders.Rtf.dll
  • DOCXTelerik.Windows.Documents.FormatProviders.OpenXml.dll
  • XAMLTelerik.Windows.Documents.FormatProviders.Xaml.dll
  • TXTTelerik.Windows.Documents.dll

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.

Export using RichTextCommand

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 EXPORT_TYPE can be html, pdf, rtf, docx, xaml, txt -->
<telerik:RadRibbonButton telerik:RadRichTextBoxRibbonUI.RichTextCommand="{Binding SaveCommand}" 
                         telerik:CommandParameter="EXPORT_TYPE" />

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. 

Export using Explicit Code

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:

// create an instance of the provider that you want to export
var customFormatProvider = new CustomFormatProvider();
   
// export the rad document to a stream
customFormatProvider.Export(radRichTextBox.Document, outputStream);

 

To export a RadRichTextBox document to different file formats, you can use this simple method:  

public void ExportContent(string defaultExtension, 
                           string filter, 
                           IDocumentFormatProvider formatProvider)
{
    var saveFileDialog = new SaveFileDialog
    {
        DefaultExt = defaultExtension, 
        Filter = filter
    };
   
    var dialogResult = saveFileDialog.ShowDialog();
    if (dialogResult == true)
    {
        using (var outputStream = saveFileDialog.OpenFile())
        {
            formatProvider.Export(radRichTextBox.Document, outputStream);
        }
    }
}

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: 

// exports to HTML file format
ExportContent(".html", "HTML File (*.html)|*.html", new HtmlFormatProvider());
   
// exports to PDF file format
ExportContent(".pdf", "PDF File (*.pdf)|*.pdf", new PdfFormatProvider());
   
// exports to RTF file format
ExportContent(".rtf", "RTF File (*.rtf)|*.rtf", new RtfFormatProvider());
   
// exports to DOCX file format
ExportContent(".docx", "DOCX File (*.docx)|*.docx", new DocxFormatProvider());
   
// exports to XAML file format
ExportContent(".xaml", "XAML File (*.xaml)|*.xaml", new XamlFormatProvider());
   
// exports to TXT file format
ExportContent(".txt", "Text File (*.txt)|*.txt", new TxtFormatProvider());
  

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.

XAML RadRichTextBox

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.

End Note

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


Get the most of XAML Download RadControls for WPF Download RadControls for Silverlight


Related Posts

Comments