Available for: UI for ASP.NET MVC | UI for ASP.NET AJAX | UI for Blazor | UI for WPF | UI for WinForms | UI for Xamarin | UI for WinUI | UI for ASP.NET Core | UI for .NET MAUI

New to Telerik Document Processing? Download free 30-day trial

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 packages listed below:

  • Telerik.Windows.Documents.Core
  • Telerik.Windows.Documents.Flow

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.

[C#] 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 you can import an HTML string.

[C#] 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

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:

[C#] 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 like shown in Example 4.

[C#] 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));

See Also

In this article