Using HtmlFormatProvider
HtmlFormatProvider allows you to import and export RadFlowDocument to/from HTML format, preserving as much as possible of the document structure and formatting. To use HtmlFormatProvider, add references to the following packages:
Telerik.Windows.Documents.CoreTelerik.Windows.Documents.Flow
Import
To import an HTML document, 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, TimeSpan.FromSeconds(10));
}
Example 2 shows how to 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, TimeSpan.FromSeconds(10));
The resulting RadFlowDocument can be used like any code-generated document.
Export
To export a document to HTML, 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, TimeSpan.FromSeconds(10));
}
You can also export the document to a string variable as 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, TimeSpan.FromSeconds(10));