65.9K
CodeProject 正在变化。 阅读更多。
Home

VB.NET 中轻松将 RTF 转换为 HTML

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.91/5 (18投票s)

2010年1月12日

CPOL

2分钟阅读

viewsIcon

170044

downloadIcon

4153

一种快速简便的解决方案,无需解析即可从 RTF 生成出色的 HTML。

引言

本文将介绍一种简单、可靠的方法,使用 VB.NET 和 Microsoft Office 自动化将富文本转换为 HTML。

背景

这一切的起因是我需要获取我开发的一个应用程序中的 RichTextBox 的内容,并将其插入到电子邮件的正文中。我们公司全面使用 Microsoft 产品,所以我可以依赖 Outlook 2007 作为所有用户的电子邮件客户端,并且我假设(错误地)我可以轻松地将富文本插入到 Outlook 电子邮件中,几乎没有问题。我真是太天真了。

当我发现 Outlook 即使在使用 Word 作为其编辑器时也不支持富文本时,我开始尝试将我的 RTF 转换为 HTML,并且我再次假设一定存在一种简单直接的方法来做到这一点,而无需自己解析所有 RTF 并考虑每个格式化标签。对互联网的全面搜索发现了一些第三方应用程序;其中一些是免费的,大多数解析了 RTF 并且似乎有些不完整,并且在简单性方面,没有一个真正符合要求。

我开始尝试使用 Office 自动化,认为如果 Microsoft 没有提供对其 RTF 到 HTML 转换过程的直接访问权限,也许他们会提供间接访问权限。果然,在用 Word 玩了一段时间后,我能够弄清楚如何使用 Word 作为翻译器,并用一个简短的函数将 RTF 直接转换为 HTML。所以,为了帮助所有正在努力解决类似问题的工薪阶层,我将介绍我是如何做到的。这里没有什么惊天动地的,但这是一个非常有用的函数,可以放在你的工具箱里。

Using the Code

基本上,只需将此函数放入你的 VB.NET 项目中即可。你需要包含对 Microsoft Word 12.0 对象库(COM 对象)的引用。其他 Word 库可能也能很好地工作,但这就是我使用的方式。

Public Function sRTF_To_HTML(ByVal sRTF As String) As String
    'Declare a Word Application Object and a Word WdSaveOptions object
    Dim MyWord As Microsoft.Office.Interop.Word.Application
    Dim oDoNotSaveChanges As Object = _
         Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges
    'Declare two strings to handle the data
    Dim sReturnString As String = ""
    Dim sConvertedString As String = ""
    Try
        'Instantiate the Word application,
        ‘set visible to false and create a document
        MyWord = CreateObject("Word.application")
        MyWord.Visible = False
        MyWord.Documents.Add()
        'Create a DataObject to hold the Rich Text
        'and copy it to the clipboard
        Dim doRTF As New System.Windows.Forms.DataObject
        doRTF.SetData("Rich Text Format", sRTF)
        Clipboard.SetDataObject(doRTF)
        'Paste the contents of the clipboard to the empty,
        'hidden Word Document
        MyWord.Windows(1).Selection.Paste()
        '…then, select the entire contents of the document
        'and copy back to the clipboard
        MyWord.Windows(1).Selection.WholeStory()
        MyWord.Windows(1).Selection.Copy()
        'Now retrieve the HTML property of the DataObject
        'stored on the clipboard
        sConvertedString = _
             Clipboard.GetData(System.Windows.Forms.DataFormats.Html)
        'Remove some leading text that shows up in some instances
        '(like when you insert it into an email in Outlook
        sConvertedString = _
             sConvertedString.Substring(sConvertedString.IndexOf("<html"))
        'Also remove multiple  characters that somehow end up in there
        sConvertedString = sConvertedString.Replace("Â", "")
        '…and you're done.
        sReturnString = sConvertedString
        If Not MyWord Is Nothing Then
            MyWord.Quit(oDoNotSaveChanges)
            MyWord = Nothing
        End If
    Catch ex As Exception
        If Not MyWord Is Nothing Then
            MyWord.Quit(oDoNotSaveChanges)
            MyWord = Nothing
        End If
        MsgBox("Error converting Rich Text to HTML")
    End Try
    Return sReturnString
End Function

'
'That does it. If you need to insert your HTML into an
'Outlook mail message (as I did) here's how to do it using the function above.
'
Dim myotl As Microsoft.Office.Interop.Outlook.Application
Dim myMItem As Microsoft.Office.Interop.Outlook.MailItem
myotl = CreateObject("Outlook.application")
myMItem = myotl.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem)
myMItem.Subject = 
    "This email was converted from rich text to HTML using a simple function in VB.net"
myMItem.Display(False)
myMItem.BodyFormat = Microsoft.Office.Interop.Outlook.OlBodyFormat.olFormatHTML
myMItem.HTMLBody = sConvertedString

关注点

有一个警告,此转换过程生成的 HTML 非常冗长。它为一些非常基本的格式生成大量的 HTML 行,但到目前为止,在这里工作时,它已经执行了数千页数据的无错误转换。

我仍然很惊讶 Microsoft 并没有在其开发库中简单地提供 RTF 到 HTML 转换功能。这似乎是一个合乎逻辑且直观的功能。不过,至少还有一个解决方法。

© . All rights reserved.