November 23, 2014
Hot Topics:

More Windows Secrets for Visual Basic

  • January 29, 2003
  • By Karl Moore
  • Send Email »
  • More Articles »

Converting RTF to HTML

One amazingly common developer request is a method of converting the contents of a RichTextBox control to HTML. But, unless you're willing to spend hundreds on a third-party text editing control, you're out of luck. Even in VB.NET, Microsoft has chosen to remain ignorant to this much desired feature.

So, you need to do it yourself—and the following chunk of code I've put together should get you started. Just pass it a RichTextBox control as a parameter and it'll return a string of HTML, ready for you to perhaps save to a file.

It doesn't handle more complicated features, such as images or tables; however, it will easily cope with fonts, sizes, colors, bold, italic, and paragraphs. Of course, you're more than welcome to expand it to suit your needs exactly.

You can call this code, as so:

strHTML = ConvertToHtml(RichTextBox1)

And here's the actual ConvertToHTML function:

Public Function ConvertToHTML(ByVal Box As RichTextBox) _
               As String
   ' Takes a RichTextBox control and returns a
   ' simple HTML-formatted version of its contents
   Dim strHTML As String
   Dim strColour As String
   Dim blnBold As Boolean
   Dim blnItalic As Boolean
   Dim strFont As String
   Dim shtSize As Short
   Dim lngOriginalStart As Long
   Dim lngOriginalLength As Long
   Dim intCount As Integer
   ' If nothing in the box, exit
   If Box.Text.Length = 0 Then Exit Function
   ' Store original selections, then select first character
   lngOriginalStart = 0
   lngOriginalLength = Box.TextLength
   Box.Select(0, 1)
   ' Add HTML header
   strHTML = "<html>"
   ' Set up initial parameters
   strColour = Box.SelectionColor.ToKnownColor.ToString
   blnBold = Box.SelectionFont.Bold
   blnItalic = Box.SelectionFont.Italic
   strFont = Box.SelectionFont.FontFamily.Name
   shtSize = Box.SelectionFont.Size
   ' Include first 'style' parameters in the HTML
   strHTML += "<span style=""font-family: " & strFont & _
     "; font-size: " & shtSize & "pt; color: " _
                     & strColour & """>"
   ' Include bold tag, if required
   If blnBold = True Then
       strHTML += "<b>"
   End If
   ' Include italic tag, if required
   If blnItalic = True Then
       strHTML += "<i>"
   End If
   ' Finally, add our first character
   strHTML += Box.Text.Substring(0, 1) 
   ' Loop around all remaining characters
   For intCount = 2 To Box.Text.Length
       ' Select current character
       Box.Select(intCount - 1, 1)
       ' If this is a line break, add HTML tag
       If Box.Text.Substring(intCount - 1, 1) = _
              Convert.ToChar(10) Then
           strHTML += "<br>"
       End If
       ' Check/implement any changes in style
       If Box.SelectionColor.ToKnownColor.ToString <> _
          strColour _ Or Box.SelectionFont.FontFamily.Name _
          <> strFont Or _ Box.SelectionFont.Size <> shtSize _
          Then
           strHTML += "</span><span style=""font-family: " _
             & Box.SelectionFont.FontFamily.Name & _
             "; font-size: " & Box.SelectionFont.Size & _
             "pt; color: " & _
             Box.SelectionColor.ToKnownColor.ToString & """>"
       End If
       ' Check for bold changes
       If Box.SelectionFont.Bold <> blnBold Then
           If Box.SelectionFont.Bold = False Then
               strHTML += "</b>"
           Else
               strHTML += "<b>"
           End If
       End If
       ' Check for italic changes
       If Box.SelectionFont.Italic <> blnItalic Then
           If Box.SelectionFont.Italic = False Then
               strHTML += "</i>"
           Else
               strHTML += "<i>"
           End If
       End If
       ' Add the actual character
       strHTML += Mid(Box.Text, intCount, 1)
       ' Update variables with current style
       strColour = Box.SelectionColor.ToKnownColor.ToString
       blnBold = Box.SelectionFont.Bold
      blnItalic = Box.SelectionFont.Italic
       strFont = Box.SelectionFont.FontFamily.Name
       shtSize = Box.SelectionFont.Size
   Next
   ' Close off any open bold/italic tags
   If blnBold = True Then strHTML += ""
   If blnItalic = True Then strHTML += ""
   ' Terminate outstanding HTML tags
   strHTML += "</span></html>"
   ' Restore original RichTextBox selection
   Box.Select(lngOriginalStart, lngOriginalLength)
   ' Return HTML
   Return strHTML
End Function
Top Tip: Looking to turn HTML back into text? Check out my "Converting HTML to Text, Easily" tip in the "More .NET Secrets" chapter of my book, VB.NET and ASP.NET Secrets for a ready-to-run algorithm.

Our code doing its stuff: translating RTF into HTML





Page 3 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel