New to Telerik UI for WinForms? 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 reference the Telerik.WinControls.RichTextEditor.dll assembly and add the following namespace:

  • Telerik.WinForms.Documents.FormatProviders.Html

Import

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

The first example shows how to use HtmlFormatProvider to import an HTML document from a file.

Import Html File

HtmlFormatProvider provider = new HtmlFormatProvider();
using (FileStream inputStream = File.OpenRead(@"..\..\RichTextEditor\ImportExport\Sample.html"))
{
    this.radRichTextEditor1.Document = provider.Import(inputStream);
}

Dim provider As HtmlFormatProvider = New HtmlFormatProvider()
Using inputStream As FileStream = File.OpenRead("..\..\RichTextEditor\ImportExport\Sample.html")
    Me.radRichTextEditor1.Document = provider.Import(inputStream)
End Using

This example shows how you can import an HTML string.

Import Html String

string html = "<p>hello world!</p>";
HtmlFormatProvider provider = new HtmlFormatProvider();
this.radRichTextEditor1.Document = provider.Import(html);

Dim html As String = "<p>hello world!</p>"
Dim provider As HtmlFormatProvider = New HtmlFormatProvider()
Me.radRichTextEditor1.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.

The first example shows how to use the HtmlFormatProvider to export an instance of RadDocument to a file:

Export Html to File

HtmlFormatProvider provider = new HtmlFormatProvider();
using (Stream output = File.OpenWrite("Sample.html"))
{
    RadDocument document = this.radRichTextEditor1.Document;
    provider.Export(document, output);
}

Dim provider As HtmlFormatProvider = New HtmlFormatProvider()
Using output As Stream = File.OpenWrite("Sample.html")
    Dim document As RadDocument = Me.radRichTextEditor1.Document
    provider.Export(document, output)
End Using

You can also export the document to a string variable like shown in the example below.

Export Html to String

RadDocument document = this.radRichTextEditor1.Document;
HtmlFormatProvider provider = new HtmlFormatProvider();
string html = provider.Export(document);

Dim document As RadDocument = Me.radRichTextEditor1.Document
Dim provider As HtmlFormatProvider = New HtmlFormatProvider()
Dim html As String = provider.Export(document)

See Also

In this article