Edit this page

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.Create(@"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);

See Also