Skip to main content

SubDocument.GetHtmlText(DocumentRange, IUriProvider) Method

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

Namespace: DevExpress.XtraRichEdit.API.Native

Assembly: DevExpress.RichEdit.v23.2.Core.dll

NuGet Packages: DevExpress.RichEdit.Core, DevExpress.Win.Navigation

Declaration

string GetHtmlText(
    DocumentRange range,
    IUriProvider provider
)

Parameters

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.

Returns

Type Description
String

A string of text in HTML format.

Remarks

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.

Example

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.

View Example

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);
        selection.EndUpdateDocument(doc);
    }
    else
    {
        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))
            Directory.CreateDirectory(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))
            Directory.CreateDirectory(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('/');
    }
}

The following code snippets (auto-collected from DevExpress Examples) contain references to the GetHtmlText(DocumentRange, IUriProvider) method.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also