Results 1 to 5 of 5

Thread: Convert RichTextBox to HTML string

Threaded View

  1. #1

    Thread Starter
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Convert RichTextBox to HTML string

    vb.net Code:
    1. Public Shared Function FromRtf(ByVal rtf As RichTextBox) As String
    2.         Dim b, i, u As Boolean
    3.         b = False : i = False : u = False
    4.         Dim fontfamily As String = "Arial"
    5.         Dim fontsize As Integer = 12
    6.         Dim htmlstr As String = String.Format("<html>{0}<body>{0}<div style=""text-align: left;""><span style=""font-family: Arial; font-size: 12pt;"">", vbCrLf)
    7.         Dim x As Integer = 0
    8.         While x < rtf.Text.Length
    9.             rtf.Select(x, 1)
    10.             If rtf.SelectionFont.Bold AndAlso (Not b) Then
    11.                 htmlstr &= "<b>"
    12.                 b = True
    13.             ElseIf (Not rtf.SelectionFont.Bold) AndAlso b Then
    14.                 htmlstr &= "</b>"
    15.                 b = False
    16.             End If
    17.             If rtf.SelectionFont.Italic AndAlso (Not i) Then
    18.                 htmlstr &= "<i>"
    19.                 i = True
    20.             ElseIf (Not rtf.SelectionFont.Italic) AndAlso i Then
    21.                 htmlstr &= "</i>"
    22.                 i = False
    23.             End If
    24.             If rtf.SelectionFont.Underline AndAlso (Not u) Then
    25.                 htmlstr &= "<u>"
    26.                 u = True
    27.             ElseIf (Not rtf.SelectionFont.Underline) AndAlso u Then
    28.                 htmlstr &= "</u>"
    29.                 u = False
    30.             End If
    31.             If fontfamily <> rtf.SelectionFont.FontFamily.Name Then
    32.                 htmlstr &= String.Format("</span><span style=""font-family: {0}; font-size: {0}pt;"">", rtf.SelectionFont.FontFamily.Name, fontsize)
    33.                 fontfamily = rtf.SelectionFont.FontFamily.Name
    34.             End If
    35.             If fontsize <> rtf.SelectionFont.SizeInPoints Then
    36.                 htmlstr &= String.Format("</span><span style=""font-family: {0}; font-size: {0}pt;"">", fontfamily, rtf.SelectionFont.SizeInPoints)
    37.                 fontsize = rtf.SelectionFont.SizeInPoints
    38.             End If
    39.             Dim curchar As String = rtf.SelectedText
    40.             Select Case curchar
    41.                 Case vbCr, vbLf : curchar = "<br />"
    42.                 Case "&" : curchar = "&amp;" : x += "&amp;".Length - 1
    43.                 Case "<" : curchar = "&lt;" : x += "&lt;".Length - 1
    44.                 Case ">" : curchar = "&gt;" : x += "&gt;".Length - 1
    45.                 Case " " : curchar = "&nbsp;" : x += "&nbsp;".Length - 1
    46.             End Select
    47.             rtf.SelectedText = curchar
    48.             x += 1
    49.         End While
    50.         Return htmlstr & String.Format("</span>{0}</body>{0}</html>", vbCrLf)
    51.     End Function
    Last edited by minitech; Apr 5th, 2010 at 06:56 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width