New to Telerik UI for WPF? Download free 30-day trial

Using HtmlFormatProvider

HtmlFormatProvider makes it easy to import and export RadDocument to/from HTML format, preserving as much as possible of the document structure and formatting.

To use HtmlFormatProvider, you should add a reference to:

  • Telerik.Windows.Documents.FormatProviders.Html.dll

Import

In order to import an HTML document you can use the overloads of the HtmlFormatProvider.Import() method.

Example 1 shows how to use HtmlFormatProvider to import an HTML document from a file.

Example 1: Import HTML file

using (Stream input = File.OpenRead(@"Sample.html")) 
{ 
    HtmlFormatProvider provider = new HtmlFormatProvider(); 
    RadDocument document = provider.Import(input); 
} 
Example 2 shows how you can import an HTML string.

Example 2: Import HTML string

string html = "<p>hello world!</p>"; 
HtmlFormatProvider provider = new HtmlFormatProvider(); 
RadDocument document = provider.Import(html); 
The resulting RadDocument can be used like any code-generated document.

Export

With the overloads of the Export() method you can export the document to an HTML string or a file.

Example 3 shows how to use the HtmlFormatProvider to export an instance of RadDocument to a file:

Example 3: Export HTML to file

HtmlFormatProvider provider = new HtmlFormatProvider(); 
using (Stream output = File.Create("Sample.html")) 
{ 
    RadDocument document = CreateRadDocument(); 
    provider.Export(document, output); 
} 
You can also export the document to a string variable like shown in Example 4.

Example 4: Export HTML to string

RadDocument document = CreateRadDocument(); 
HtmlFormatProvider provider = new HtmlFormatProvider(); 
string html = provider.Export(document); 

However, the format providers cannot be used in XAML and you have to implement a logic that will call their Import() and Export() methods. This is something that you might not want to be concerned with when using RadRichTextBox in a data bound scenarios. For such cases, the DataProvider classes are used. They wrap the FormatProviders' functionality and allow its usage in XAML.

See Also

In this article