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);
}
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);
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);
}
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);