.NET Framework 4.5.2+
.NET Framework 4.5.2+
.NET Standard 2.0+

SubDocument.GetHtmlText(DocumentRange, IUriProvider) Method

Gets the text contained in the specified range in HTML format.

Namespace: DevExpress.XtraRichEdit.API.Native

Assembly: DevExpress.RichEdit.v21.1.Core.dll


string GetHtmlText(
    DocumentRange range,
    IUriProvider provider


Name Type Description
range DocumentRange

A DocumentRange object that is the text range in the document.

provider IUriProvider

A class that implements the IUriProvider interface and provides methods which return URI for images and CSS data.


Type Description

A string of text in HTML format.


If the provider parameter is set to null, the method uses a default DataStringUriProvider that embeds images as base64-encoded data.

If you operate with a selection, enclose the GetHtmlText method within a DocumentRange.BeginUpdateDocument - DocumentRange.EndUpdateDocument method pair. Otherwise, you may get an error “Specified document position or range belongs to other document or subdocument“.

To retrieve the section settings, make sure that the range contains the last section’s paragraph. Otherwise, the section settings are reset to the default.


This code snippet illustrates how to export the selected range to the string in HTML format.

The RichEditControl.BeforeExport event is handled to specify the required export options. However, you can use the SubDocument.GetHtmlText method overload to pass options to the method.

A custom class that implements the IUriProvider interface provides names and locations for images and CSS-files in HTML output.

private void btnHtmlCustomUri_Click(object sender, EventArgs e)
    CustomUriProvider uriProvider = new CustomUriProvider(Path.GetDirectoryName(fileName));
    richEditControl.BeforeExport += richEditControl_BeforeExport;

    if (this.richEditControl.Document.Selection.Length > 0)
        DocumentRange selection = richEditControl.Document.Selection;
        SubDocument doc = selection.BeginUpdateDocument();
        htmlText = doc.GetHtmlText(selection, uriProvider);
        htmlText = richEditControl.Document.GetHtmlText(richEditControl.Document.Range, uriProvider);
    richEditControl.BeforeExport -= richEditControl_BeforeExport;

private void richEditControl_BeforeExport(object sender, DevExpress.XtraRichEdit.BeforeExportEventArgs e)
    DevExpress.XtraRichEdit.Export.HtmlDocumentExporterOptions options = e.Options as DevExpress.XtraRichEdit.Export.HtmlDocumentExporterOptions;
    if (options != null) {
        options.CssPropertiesExportType = DevExpress.XtraRichEdit.Export.Html.CssPropertiesExportType.Link;
        options.HtmlNumberingListExportFormat = DevExpress.XtraRichEdit.Export.Html.HtmlNumberingListExportFormat.HtmlFormat;
        options.TargetUri = Path.GetFileNameWithoutExtension(this.fileName);

public class CustomUriProvider : DevExpress.Office.Services.IUriProvider
    string rootDirectory;
    public CustomUriProvider(string rootDirectory)
        if (String.IsNullOrEmpty(rootDirectory))
            DevExpress.Office.Utils.Exceptions.ThrowArgumentException("rootDirectory", rootDirectory);
        this.rootDirectory = rootDirectory;

    public string CreateCssUri(string rootUri, string styleText, string relativeUri)
        string cssDir = String.Format("{0}\\{1}", this.rootDirectory, rootUri.Trim('/'));
        if (!Directory.Exists(cssDir))
        string cssFileName = String.Format("{0}\\style.css", cssDir);
        File.AppendAllText(cssFileName, styleText);
        return GetRelativePath(cssFileName);
    public string CreateImageUri(string rootUri, DevExpress.Office.Utils.OfficeImage image, string relativeUri)
        string imagesDir = String.Format("{0}\\{1}", this.rootDirectory, rootUri.Trim('/'));
        if (!Directory.Exists(imagesDir))
        string imageName = String.Format("{0}\\{1}.png", imagesDir, Guid.NewGuid());
        image.NativeImage.Save(imageName, ImageFormat.Png);
        return GetRelativePath(imageName);
    string GetRelativePath(string path)
        string substring = path.Substring(this.rootDirectory.Length);
        return substring.Replace("\\", "/").Trim('/');
See Also