Click here to Skip to main content
Click here to Skip to main content

Using a VB.NET 2005 Class to Convert RichText to HTML

By , 2 Nov 2006
Rate this:
Please Sign up or sign in to vote.

A window showing a Rich Editor and the resutling HTML code

Introduction

This code was written to solve the problem of having RichText data needing to be displayed in HTML, both in HTML reports and in ASP.NET web pages.

Using the code

Use of the code is exceptionally simple. Simply create a new instance of the class and then use the properties to first pass in the RichText to parse and then use the html property to get the resulting HTML. (The code file for the class is attached to this article).

''Assume you have a RichTextBox on your 
''form named rtbFoo and a button named btnBar

  Private Sub btnBar_Click(ByVal sender as object, _
          e as System.EventArgs) Handles btnBar.Click
    dim r2h as new RTFtoHTML
    r2h.rtf = rtbFoo.rtf
    messagebox.show(r2h.html)
  End Sub

History

  • Initial release - 11/02/2006.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

George H. Slaterpryce III
Architect
United States United States
Developing software since 1995, starting out with Borland Turbo C++ 3 & Motorolla Assembly.
Eventually learning several different languages such as
Pascal/Delphi,Basic, Visual Basic,FoxPro, ASP, PHP, and Perl.
 
Currently using the .net framework for most development task.
 
Hobbies include underwater robotics, and electronics.
 
George's current language of choice is C#.

Comments and Discussions

 
GeneralMy vote of 3 Pinmemberhepter3-Jan-14 23:32 
QuestionAre you kidding me!? PinmemberFuck you assholes30-Jul-12 5:09 
AnswerRe: Are you kidding me!? PinmemberGeorge H. Slaterpryce III30-Jul-12 10:07 
Questionc# PinmemberJheiron29-Jun-12 7:51 
AnswerRe: c# PinmemberGeorge H. Slaterpryce III30-Jul-12 10:08 
Questionhow can I use with Strike tag? Pinmemberkals8420-Dec-11 1:53 
QuestionNew Release PinmemberGeorge H. Slaterpryce III5-Dec-11 21:18 
AnswerRe: New Release PinmemberChitation4-Jan-12 11:31 
AnswerRe: New Release Pinmember98z2831-Mar-12 11:35 
AnswerRe: New Release Pinmembersimon Barnby9-Aug-12 22:00 
AnswerRe: New Release Pinmemberdotnetme227-Sep-13 5:28 
Answervery nice code & good readers hint. Pinmembernasimb16-Nov-11 3:00 
Questiondisplay html format in vb.net Pinmemberepit2429-Sep-11 17:04 
QuestionFix for .NET4 and VS2010 PinmemberJohnnyasdf11-Sep-11 12:36 
BugA little bug? Pinmember98z2813-Aug-11 8:38 
GeneralRe: A little bug? PinmemberGeorge H. Slaterpryce III15-Aug-11 16:31 
GeneralRe: A little bug? Pinmember98z2817-Aug-11 9:18 
GeneralRe: A little bug? PinmemberJohnnyasdf10-Sep-11 13:54 
QuestionConversion of text in textbox to HTML file PinmemberAdarshPatil25-Nov-10 19:35 
GeneralUnderline Pinmemberkheonline6-Oct-10 4:30 
QuestionCombined with RTBExVB Pinmemberbigbro_19858-Aug-10 20:29 
AnswerRe: Combined with RTBExVB PinmemberGeorge H. Slaterpryce III3-Sep-10 9:57 
GeneralCode for changing the Font and the FontSize with ListBox Pinmembervb_newsbie12-Jul-10 21:14 
GeneralRe: Code for changing the Font and the FontSize with ListBox Pinmembervb_newsbie14-Jul-10 0:16 
GeneralHi George Slaterpryce III PinmemberCurtis Underwood17-May-10 6:38 
I Really Like your code.
   I have made some modifications to your code as per your permissions granted. These Modifications include adding spaces so the converted HTML is spaced exactly like the RTF is spaced. I discovered that after converting and placing in a HTML document it was all Left aligned. I also found that when copying the RTF from VB IDE that if the VB code referenced HTML formatting that it used the references to HTML. So I made a modification to prevent this. I am at present looking into a way to change the embedded objects to their respective Image or Thumb nails. The only way I could get this to post the code correctly Was to unchecked "Use HTML in post" and unchecked Encode. Because it caused the HTML code " " to change to a " ". Wrapping it did nothing to help.
But here is the code the way it is modified starting from Alignment Which was not modified.
<pre>
                  'Alignment
                  If intPos = 0 Then
                        strReturn &= "<p style=""text-align:" & _rtfSource.SelectionAlignment.ToString & """>" '
                        altCurrent = _rtfSource.SelectionAlignment
                  Else
                        If _rtfSource.SelectionAlignment <> altCurrent Then
                              strReturn &= "</p>"
                              strReturn &= "<p style=""text-align:" & _rtfSource.SelectionAlignment.ToString & """>"
                              altCurrent = _rtfSource.SelectionAlignment
                        End If
                  End If
                  '"   (1) Modified to allow spaces and Html control syntex to be displayed(Curtis Underwood,April/29/2010 )
                  If intPos > 1 Then
                        If _rtfSource.Text.Substring(intPos, 1) = CStr(Chr(60)) And _rtfSource.Text.Substring(intPos - 1, 1) = CStr(Chr(34)) Then
                              strReturn &= "&lt;"
                              intPos += 1
                              Do Until _rtfSource.Text.Substring(intPos, 1) = CStr(Chr(34)) Or _rtfSource.Text.Substring(intPos, 1) = CStr(Chr(62))
                                    strReturn &= _rtfSource.Text.Substring(intPos, 1)
                                    intPos += 1
                              Loop
                        End If
                  ElseIf intPos < _rtfSource.Text.Length Then
                        If _rtfSource.Text.Substring(intPos, 1) = CStr(Chr(62)) And _rtfSource.Text.Substring(intPos + 1, 1) = CStr(Chr(34)) Then
                              strReturn &= "&gt;"
                        End If
                  End If
 
                  If _rtfSource.Text.Substring(intPos, 1) = " " Then
                        strReturn &= "&nbsp;"
                  Else
                        strReturn &= _rtfSource.Text.Substring(intPos, 1)
                  End If
            Next</pre>

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 2 Nov 2006
Article Copyright 2006 by George H. Slaterpryce III
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid