Using HtmlFormatProvider
HtmlFormatProvider makes it easy to import and export RadFlowDocument to/from HTML format, preserving as much as possible of the document structure and formatting. To use HtmlFormatProvider, you should add references to the assemblies listed below:
- Telerik.Windows.Documents.Core.dll
- Telerik.Windows.Documents.Flow.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"))
{
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
RadFlowDocument 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>";
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
RadFlowDocument document = provider.Import(html);
The resulting RadFlowDocument can be used like any code-generated document.
Export
In order to export a document to HTML you can use the overloads of the HtmlFormatProvider.Export() method.
Example 3 shows how to use the HtmlFormatProvider to export an instance of RadFlowDocument to a file:
Example 3: Export HTML to file
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
using (Stream output = File.Create("Sample.html"))
{
RadFlowDocument document = CreateRadFlowDocument(); // CreateRadFlowDocument() is a custom method that creates a simple instance of RadFlowDocument. You can replace it with the instance you would like to export.
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
RadFlowDocument document = CreateRadFlowDocument(); // CreateRadFlowDocument() is a custom method that creates a simple instance of RadFlowDocument. You can replace it with the instance you would like to export.
Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();
string html = provider.Export(document);