Back to Devexpress

SubDocument.GetHtmlText(DocumentRange, IUriProvider) Method

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

latest14.4 KB
Original Source

SubDocument.GetHtmlText(DocumentRange, IUriProvider) 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
)
vb
Function GetHtmlText(
    range As DocumentRange,
    provider As IUriProvider
) 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.

|

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.

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

csharp
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('/');
    }
}
vb
Private Sub btnHtmlCustomUri_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim uriProvider As New CustomUriProvider(Path.GetDirectoryName(fileName))
    AddHandler richEditControl.BeforeExport, AddressOf richEditControl_BeforeExport

    If Me.richEditControl.Document.Selection.Length > 0 Then
        Dim selection As DocumentRange = richEditControl.Document.Selection
        Dim doc As SubDocument = selection.BeginUpdateDocument()
        htmlText = doc.GetHtmlText(selection, uriProvider)
        selection.EndUpdateDocument(doc)
    Else
        htmlText = richEditControl.Document.GetHtmlText(richEditControl.Document.Range, uriProvider)
    End If
    RemoveHandler richEditControl.BeforeExport, AddressOf richEditControl_BeforeExport
End Sub

Private Sub richEditControl_BeforeExport(ByVal sender As Object, ByVal e As DevExpress.XtraRichEdit.BeforeExportEventArgs)
    Dim options As DevExpress.XtraRichEdit.Export.HtmlDocumentExporterOptions = TryCast(e.Options, DevExpress.XtraRichEdit.Export.HtmlDocumentExporterOptions)
    If options IsNot Nothing Then
        options.CssPropertiesExportType = DevExpress.XtraRichEdit.Export.Html.CssPropertiesExportType.Link
        options.HtmlNumberingListExportFormat = DevExpress.XtraRichEdit.Export.Html.HtmlNumberingListExportFormat.HtmlFormat
        options.TargetUri = Path.GetFileNameWithoutExtension(Me.fileName)
    End If
End Sub

Public Class CustomUriProvider
    Implements DevExpress.Office.Services.IUriProvider

    Private rootDirectory As String
    Public Sub New(ByVal rootDirectory As String)
        If String.IsNullOrEmpty(rootDirectory) Then
            DevExpress.Office.Utils.Exceptions.ThrowArgumentException("rootDirectory", rootDirectory)
        End If
        Me.rootDirectory = rootDirectory
    End Sub

    Public Function CreateCssUri(ByVal rootUri As String, ByVal styleText As String, ByVal relativeUri As String) As String
        Dim cssDir As String = String.Format("{0}\{1}", Me.rootDirectory, rootUri.Trim("/"c))
        If Not Directory.Exists(cssDir) Then
            Directory.CreateDirectory(cssDir)
        End If
        Dim cssFileName As String = String.Format("{0}\style.css", cssDir)
        File.AppendAllText(cssFileName, styleText)
        Return GetRelativePath(cssFileName)
    End Function

    Public Function CreateImageUri(ByVal rootUri As String, ByVal image As DevExpress.Office.Utils.OfficeImage, ByVal relativeUri As String) As String
        Dim imagesDir As String = String.Format("{0}\{1}", Me.rootDirectory, rootUri.Trim("/"c))
        If Not Directory.Exists(imagesDir) Then
            Directory.CreateDirectory(imagesDir)
        End If
        Dim imageName As String = String.Format("{0}\{1}.png", imagesDir, Guid.NewGuid())
        image.NativeImage.Save(imageName, ImageFormat.Png)
        Return GetRelativePath(imageName)
    End Function
    Private Function GetRelativePath(ByVal path As String) As String
        Dim substring As String = path.Substring(Me.rootDirectory.Length)
        Return substring.Replace("\", "/").Trim("/"c)
    End Function
End Class

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.

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

csharp
DevExpress.XtraRichEdit.API.Native.SubDocument doc = selection.BeginUpdateDocument();
htmlText = doc.GetHtmlText(selection, uriProvider);
selection.EndUpdateDocument(doc);

winforms-richedit-build-a-mail-application/CS/RichEditSendMail/Form1.cs#L80

csharp
control.BeforeExport += OnBeforeExport;
string htmlBody = control.Document.GetHtmlText(control.Document.Range, this);
AlternateView view = AlternateView.CreateAlternateViewFromString(htmlBody, Encoding.UTF8, MediaTypeNames.Text.Html);

winforms-richedit-export-the-document-into-an-outlook-mail-item/CS/Form1.cs#L63

csharp
control.BeforeExport += OnBeforeExport;
string htmlBody = control.Document.GetHtmlText(control.Document.Range, this);
control.BeforeExport -= OnBeforeExport;

word-document-api-send-mail-merge-document-as-email/CS/RichEditMailMessageExporter.cs#L35

csharp
server.BeforeExport += OnBeforeExport;
string htmlBody = server.Document.GetHtmlText(server.Document.Range, this);
server.BeforeExport -= OnBeforeExport;

winforms-richedit-document-api/CS/RichEditAPISample/CodeExamples/Export.cs#L36

csharp
// Export to HTML.
string htmlText = document.GetHtmlText(r, null);
File.WriteAllText("test.html", htmlText);

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

vb
Dim doc As DevExpress.XtraRichEdit.API.Native.SubDocument = selection.BeginUpdateDocument()
htmlText_Renamed = doc.GetHtmlText(selection, uriProvider)
selection.EndUpdateDocument(doc)

winforms-richedit-build-a-mail-application/VB/RichEditSendMail/Form1.vb#L74

vb
AddHandler control.BeforeExport, AddressOf OnBeforeExport
Dim htmlBody As String = control.Document.GetHtmlText(control.Document.Range, Me)
Dim view As AlternateView = AlternateView.CreateAlternateViewFromString(htmlBody, Encoding.UTF8, MediaTypeNames.Text.Html)

winforms-richedit-export-the-document-into-an-outlook-mail-item/VB/Form1.vb#L63

vb
AddHandler control.BeforeExport, AddressOf OnBeforeExport
Dim htmlBody As String = control.Document.GetHtmlText(control.Document.Range, Me)
RemoveHandler control.BeforeExport, AddressOf OnBeforeExport

word-document-api-send-mail-merge-document-as-email/VB/RichEditMailMessageExporter.vb#L33

vb
AddHandler server.BeforeExport, AddressOf OnBeforeExport
Dim htmlBody As String = server.Document.GetHtmlText(server.Document.Range, Me)
RemoveHandler server.BeforeExport, AddressOf OnBeforeExport

winforms-richedit-document-api/VB/RichEditAPISample/CodeExamples/Export.vb#L32

vb
' Export to HTML.
Dim htmlText As String = document.GetHtmlText(r, Nothing)
Call System.IO.File.WriteAllText("test.html", htmlText)

See Also

SubDocument Interface

SubDocument Members

DevExpress.XtraRichEdit.API.Native Namespace