Settings
HtmlFormatProvider allows you to import HTML documents and export a RadFlowDocument to HTML. The import and export settings let you modify the provider behavior. This article outlines the available settings.
Import Settings
The HtmlImportSettings class represents the import settings. You can specify the following options:
DefaultStyleSheet
The property specifies the default stylesheet that the provider uses. This is equivalent to the browser's "user agent stylesheet" and specifies the formatting that applies by default to the elements (before all styles that the document explicitly defines).
The default value, for example, specifies that b elements are imported with font-weight: bold.
You can set the value of the property to any valid CSS document. Example 1 shows how you can add styling besides the default styles.
Example 1: Add CSS to the DefaultStyleSheet
HtmlFormatProvider html_provider = new HtmlFormatProvider();
html_provider.ImportSettings.DefaultStyleSheet = provider.ImportSettings.DefaultStyleSheet + @" p {color: #ff99ac;}";
GenericFonts
The property contains an instance of the GenericHtmlFonts class, which specifies the ThemableFontFamily objects to use when importing the five generic CSS font families: serif, sans-serif, monospace, cursive, and fantasy. By default, these fonts are Times New Roman, Arial, Courier New, Comic Sans MS, and Algerian, respectively.
LoadImageFromUri and LoadStyleSheetFromUri Events
These events allow you to load external resources (images and CSS files) that the HTML does not include. To use these events, load the data from the URI in the event arguments and return it.
The
LoadFromUrievent is obsolete starting with R1 2021. UseLoadImageFromUriorLoadStyleSheetFromUriinstead. Starting with Q3 2015, the library introduced theUriImageSourceclass. This class automatically downloads the images. But because such an approach poses a security risk, it is obsolete starting with R1 2021.
The LoadImageFromUri event uses the LoadImageFromUriEventArgs object which exposes the following properties:
Uri: The URI originally specified in the imported HTML file.SetImageInfo: Passes the image data and image format (extension).
Example 2 shows how to use the LoadImageFromUri event to download an image.
Example 2: Use the LoadImageFromUri
HtmlFormatProvider provider = new HtmlFormatProvider();
HtmlImportSettings importSettings = new HtmlImportSettings();
importSettings.LoadImageFromUri += (s, e) =>
{
// Load the data representing the resource
System.Net.WebClient webClient = new System.Net.WebClient();
byte[] data = webClient.DownloadData(e.Uri);
// Pass the loaded data to the arguments
string extension = System.IO.Path.GetExtension(e.Uri).Substring(1); // Get the extension without the dot
e.SetImageInfo(data, extension);
};
provider.ImportSettings = importSettings;
The LoadStyleSheetFromUri event uses the LoadStyleSheetFromUriEventArgs object which exposes the following properties:
Uri: The URI originally specified in the imported HTML file.SetStyleSheetContent: Passes the styles as a string.
Example 3 shows how to use the LoadStyleSheetFromUri event.
Example 3: Use the LoadStyleSheetFromUri event
HtmlFormatProvider provider = new HtmlFormatProvider();
HtmlImportSettings importSettings = new HtmlImportSettings();
importSettings.LoadStyleSheetFromUri += (s, e) =>
{
string styles = File.ReadAllText(@"Data\" + e.Uri);
e.SetStyleSheetContent(styles);
};
provider.ImportSettings = importSettings;
UriImageSource Class
When importing HTML that contains images with URI source, the HtmlFormatProvider creates a UriImageSource object for each of these images. UriImageSource downloads the image data on demand and exposes the following properties:
Uri: Gets the URI of the image.Data: Gets a byte array that represents the image data. When you access this property, the class downloads the image data.Extension: Gets the extension. When you access this property, the class downloads the image data only if the extension is not predefined.
The
UriImageSourceclass and theLoadFromUrievent make HTTP requests and download the respective resources. Consider this if your application must not freely access remote resources.
When you export a
UriImageSourceobject to other supported formats, the provider downloads the image and saves it according to the target format specification.
Converting UriImageSource to ImageSource
The provider always exports UriImageSource objects as images with URI as their source, regardless of the export settings. If you need to export this object as an embedded or external image, convert the UriImageSource to an ImageSource object.
Example 4: Convert UriImageSource to ImageSource
UriImageSource uriImageSource = imageInline.Image.ImageSource as UriImageSource;
if (uriImageSource != null)
{
imageInline.Image.ImageSource = new ImageSource(uriImageSource.Data, uriImageSource.Extension);
}
Export Settings
The HtmlExportSettings class represents the export settings. You can specify the following settings:
BordersMinimalThickness
Specifies the minimal exported thickness of the borders of tables and table cells. This can be useful because some browsers cannot render thin borders properly (below 1px or similar). The default value is 0, which means the provider exports all borders with their actual thickness.
DocumentExportLevel
Specifies the export level of the document through the DocumentExportLevel enumeration:
Document: Exports a full HTML document. This is the default value of the property.Fragment: Exports a fragment of the HTML document—only thebodytag.
IndentDocument
Specifies whether the exported document is indented (formatted with spaces). The default value is false, as this ensures minimum size rather than human-readability.
ImagesExportMode
This property controls how the provider exports images. The available options are external file and embedded image.
When you import an image with URI source, the
ImageSourceproperty of the Image contains an object of typeUriImageSource. As a result, the provider always exports the image with URI and does not respect theImagesExportModeproperty. If you need the provider to respect this property, convert theUriImageSourceobject toImageSource.
When you work with relative URIs, the import and export features only preserve the path to the image and do not change it.
ImagesFolderPath
This property specifies the folder path where the provider exports external images. This property applies only when ImageExportMode is set to External.
ImagesSourceBasePath
Specifies the base path for the src attribute of the exported image tags. This property applies only when the ImageExportMode is set to External.
ImageExporting
The provider raises this event for every image in the document. The event argument exposes the following options:
Title: Allows you to set thetitleattribute of theimageelement.AlternativeText: Can be used to specify the content of thealtattribute of the image. It is null by default and the attribute is not exported.Handled: Marks the event as handled. IfImagesExportModeis set toExternaland the event is marked as handled, the format provider does not attempt to export the image to an external image file.Image: Can be used to get the original Image object, from where you can access image name, image data, and image extension.Source: Can be used to specify the value of thesrcattribute of theimageelement. IfImagesExportModeis set toExternal, the source can be set to absolute or to a relative path pointing to the image file.ExportSize: Use this property to specify whether thewidthandheightattributes of animageelement are exported. The default value istrue.
StylesExportMode
Specifies the export mode for the styles in the RadFlowDocument StyleRepository, through the StylesExportMode enumeration:
Embedded:RadFlowDocumentstyles are exported as internal CSS—selectors embedded into the HTML instyleelement in theheadsection. This is the default value of the property.External:RadFlowDocumentstyles are exported as CSS selectors in external CSS file. You have two options:- Set
StylesFilePathandStylesSourcePathproperties, which are used to export the external file. - Handle
ExportStylesToExternalSourceevent, specifying the output stream and reference URI to be exported.
- Set
Inline:RadFlowDocumentstyles are exported as CSS properties in thestyleattribute of the HTML elements. This ensures maximum compatibility with some applications that don't support external or embedded CSS, for example, some email clients.None:RadFlowDocumentstyles are not exported. The local values of style properties of the DocumentElements are still exported as CSS properties in thestyleattribute of the HTML elements.
StylesFilePath
This property specifies the file path where the external styles are exported.
StylesSourcePath
The property specifies the base path for the href attribute of the link element pointing to the file that contains the external styles.
ExternalStylesExporting
The event is only raised when the StylesExportMode property is set to External. The event argument exposes the following properties:
Css: String that contains the actual CSS that is exported.Handled: Marks the event as handled. If the event is marked as handled, the format provider does not attempt to export the CSS file.Reference: The value that is set to thehrefattribute of thelinkelement tag. In general, this is set to the absolute or relative path pointing to the file that contains the CSS styles.
Example 5 shows how to create export settings.
Example 5: Create HtmlExportSettings
HtmlFormatProvider html_provider = new HtmlFormatProvider();
HtmlExportSettings exportSettings = new HtmlExportSettings();
byte[] data = null;
exportSettings.BordersMinimalThickness = 1;
exportSettings.DocumentExportLevel = DocumentExportLevel.Fragment;
exportSettings.IndentDocument = true;
exportSettings.ImageExporting += (s, e) =>
{
e.Source = "test.jpg";
data = e.Image.ImageSource.Data;
e.Handled = true;
e.Title = "Test image";
e.ExportSize = true;
e.AlternativeText = "You will see this text if the image is not loaded";
};
html_provider.ExportSettings = exportSettings;