Back to Devexpress

SubDocument.GetHtmlText(DocumentRange, IUriProvider, HtmlDocumentExporterOptions) Method

officefileapi-devexpress-dot-xtrarichedit-dot-api-dot-native-dot-subdocument-dot-gethtmltext-x28-documentrange-iuriprovider-htmldocumentexporteroptions-x29.md

latest11.5 KB
Original Source

SubDocument.GetHtmlText(DocumentRange, IUriProvider, HtmlDocumentExporterOptions) Method

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

Namespace : DevExpress.XtraRichEdit.API.Native

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

NuGet Package : DevExpress.RichEdit.Core

Declaration

csharp
string GetHtmlText(
    DocumentRange range,
    IUriProvider provider,
    HtmlDocumentExporterOptions options
)
vb
Function GetHtmlText(
    range As DocumentRange,
    provider As IUriProvider,
    options As HtmlDocumentExporterOptions
) As String

Parameters

NameTypeDescription
rangeDocumentRange

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.

| | options | HtmlDocumentExporterOptions |

A HtmlDocumentExporterOptions instance containing various options.

|

Returns

TypeDescription
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. In this situation the options parameter is disregarded.

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

The following code snippet uses the DocumentImage.Uri property to set the image’s src attribute when a document is saved in HTML format. You can switch on the HtmlDocumentExporterOptions.EmbedImages option to observe that the custom URI provider is idle for embedded images.

View Example: Retain the Original Image URI in an HTML Document

csharp
using DevExpress.Office.Services;
using DevExpress.XtraRichEdit.API.Native;
using DevExpress.XtraRichEdit.Export;
using System;
// ...
        private void richEditControl1_DocumentLoaded(object sender, EventArgs e)
        {
            IUriProviderService service = richEditControl1.GetService<IUriProviderService>();
            if (service != null) {
                service.RegisterProvider(new CustomUriProvider());
            }
        }
        private void richEditControl1_ContentChanged(object sender, EventArgs e)
        {
            ReloadHtml();
        }

        private void ReloadHtml()
        {
            HtmlDocumentExporterOptions exportOptions = new HtmlDocumentExporterOptions();
            exportOptions.EmbedImages = embedImagesCheck.Checked;
            string sText = richEditControl1.Document.GetHtmlText(richEditControl1.Document.Range, new CustomUriProvider(), exportOptions);
            memoEdit1.Text = sText;
        }
vb
Imports DevExpress.Office.Services
Imports DevExpress.XtraRichEdit.API.Native
Imports DevExpress.XtraRichEdit.Export
Imports System
' ...
        Private Sub richEditControl1_DocumentLoaded(ByVal sender As Object, ByVal e As EventArgs) Handles richEditControl1.DocumentLoaded
            Dim service As IUriProviderService = richEditControl1.GetService(Of IUriProviderService)()
            If service IsNot Nothing Then
                service.RegisterProvider(New CustomUriProvider())
            End If
        End Sub

        Private Sub richEditControl1_ContentChanged(ByVal sender As Object, ByVal e As EventArgs)
            ReloadHtml()
        End Sub

        Private Sub ReloadHtml()
            Dim exportOptions As New HtmlDocumentExporterOptions()
            exportOptions.EmbedImages = embedImagesCheck.Checked
            Dim sText As String = richEditControl1.Document.GetHtmlText(richEditControl1.Document.Range, New CustomUriProvider(), exportOptions)
            memoEdit1.Text = sText
        End Sub
csharp
using DevExpress.Office.Services;
using DevExpress.Office.Utils;
using System;
// ...
     public class CustomUriProvider : IUriProvider
     {
        public string CreateCssUri(string rootUri, string styleText, string relativeUri)
        {
            return String.Empty;
        }

        public string CreateImageUri(string rootUri, OfficeImage image, string relativeUri)
        {
            return image.Uri;
        }
    }
vb
Imports DevExpress.Office.Services
Imports DevExpress.Office.Utils
Imports System
' ...
    Public Class CustomUriProvider
        Implements IUriProvider

        Public Function CreateCssUri(ByVal rootUri As String, ByVal styleText As String, ByVal relativeUri As String) As String Implements IUriProvider.CreateCssUri
            Return String.Empty
        End Function

        Public Function CreateImageUri(ByVal rootUri As String, ByVal image As OfficeImage, ByVal relativeUri As String) As String Implements IUriProvider.CreateImageUri
            Return image.Uri
        End Function
    End Class

The following code snippets (auto-collected from DevExpress Examples) contain references to the GetHtmlText(DocumentRange, IUriProvider, HtmlDocumentExporterOptions) 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.

winforms-richedit-save-text-from-a-range-in-different-formats/CS/GetTextMethodsExample/Form1.cs#L93

csharp
DevExpress.XtraRichEdit.API.Native.SubDocument doc = selection.BeginUpdateDocument();
myBrowser.SetHtml(doc.GetHtmlText(selection, null, myExportOptions));
selection.EndUpdateDocument(doc);

winforms-rich-text-editor-retain-original-image-uri-in-html-document/CS/Form1.cs#L31

csharp
exportOptions.EmbedImages = embedImagesCheck.Checked;
string sText = richEditControl1.Document.GetHtmlText(richEditControl1.Document.Range, new CustomUriProvider(), exportOptions);
memoEdit1.Text = sText;

how-to-customize-copy-and-paste-commands/CS/RichEditCustomCopyPaste/CustomCommands.cs#L25

csharp
try {
    string html = Control.Document.GetHtmlText(Control.Document.Selection, new CustomUriProvider(), richEditControl.Options.Export.Html);
    htmlForClipboard = CF_HTMLHelper.GetHtmlClipboardFormat(html);

word-processing-customize-html-export/CS/Program.cs#L71

csharp
var uriProvider = new MyUriProvider(Path.GetDirectoryName(Application.StartupPath));
    stringHtml = document.GetHtmlText(document.Range, uriProvider, options);
}

winforms-richedit-save-text-from-a-range-in-different-formats/VB/GetTextMethodsExample/Form1.vb#L72

vb
Dim doc As DevExpress.XtraRichEdit.API.Native.SubDocument = selection.BeginUpdateDocument()
myBrowser.SetHtml(doc.GetHtmlText(selection, Nothing, myExportOptions))
selection.EndUpdateDocument(doc)

winforms-rich-text-editor-retain-original-image-uri-in-html-document/VB/Form1.vb#L27

vb
exportOptions.EmbedImages = embedImagesCheck.Checked
Dim sText As String = richEditControl1.Document.GetHtmlText(richEditControl1.Document.Range, New CustomUriProvider(), exportOptions)
memoEdit1.Text = sText

how-to-customize-copy-and-paste-commands/VB/CustomCommands.vb#L27

vb
Try
    Dim html As String = Control.Document.GetHtmlText(Control.Document.Selection, New CustomUriProvider(), richEditControl.Options.Export.Html)
    htmlForClipboard = CF_HTMLHelper.GetHtmlClipboardFormat(html)

word-processing-customize-html-export/VB/Program.vb#L69

vb
Dim uriProvider As New MyUriProvider(Path.GetDirectoryName(Application.StartupPath))
    stringHtml = document.GetHtmlText(document.Range, uriProvider, options)
End Sub

See Also

How to save the document range in different formats

SubDocument Interface

SubDocument Members

DevExpress.XtraRichEdit.API.Native Namespace