# Using HtmlFormatProvider

Telerik Document Processing includes the `HtmlFormatProvider` class in **RadWordsProcessing** for importing HTML to `RadFlowDocument` and exporting `RadFlowDocument` to HTML. The provider maps supported HTML elements and CSS properties to the flow document model. The class is in the `Telerik.Documents.Flow.FormatProviders.Html` namespace. To use `HtmlFormatProvider`, reference the following packages:

* `Telerik.Documents.Core`
* `Telerik.Documents.Flow`

If your project uses the Windows-only package family, use the corresponding `Telerik.Windows.Documents.*` package names instead.

`HtmlFormatProvider` exposes [`ImportSettings`](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/formats-and-conversion/html/settings#import-settings) and [`ExportSettings`](https://www.telerik.com/document-processing-libraries/documentation/libraries/radwordsprocessing/formats-and-conversion/html/settings#export-settings) properties. Use `HtmlImportSettings` to handle external images and stylesheets through the `LoadImageFromUri` and `LoadStyleSheetFromUri` events. Use `HtmlExportSettings` to control options such as `ImagesExportMode`, `StylesExportMode`, and `DocumentExportLevel`.

## 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 an HTML File**

```C#
using (Stream input = File.OpenRead(@"Sample.html"))
{
    Telerik.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.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 an HTML String**

```C#
string html = "<p>hello world!</p>";
Telerik.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();

RadFlowDocument document = provider.Import(html, TimeSpan.FromSeconds(10));
```

The resulting `RadFlowDocument` works like any document that you create in code.

## 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 a RadFlowDocument to an HTML File**

```C#
Telerik.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.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 a RadFlowDocument to an HTML String**

```C#
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.Documents.Flow.FormatProviders.Html.HtmlFormatProvider provider = new Telerik.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();

string html = provider.Export(document, TimeSpan.FromSeconds(10));
```

## See Also

* [Resolving FileNotFoundException for System.Text.Encoding.CodePages in RadWordsProcessing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/resolve-file-not-found-exception-system-text-encoding-codepages-radwordsprocessing)
* [Timeout Mechanism](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/timeout-mechanism-in-dpl)
* [Converting HTML to Image Using Telerik Document Processing](https://www.telerik.com/document-processing-libraries/documentation/knowledge-base/converting-html-to-image-using-document-processing)
