Hi.
I'm trying to convert Brady Hegberg's great RTF2HTML[1] VB 6.0
module to C#. I've managed to convert the VB code to VB.NET,
which gave me the following code:
Option Strict On
Option Explicit On
Option Compare Binary
Public Class RtfToHtml
'Version 3.03
'Copyright Brady Hegberg 2000
' I'm not licensing this software but I'd appreciate it if
' you'd to consider it to be under an lgpl sort of license
'More information can be found at
'
http://www2.bitstream.net/~bradyh/downloads/rtf2htmlrm.html
'Converts Rich Text encoded text to HTML format
'if you find some text that this function doesn't
'convert properly please email the coded text to
'bradyh@bitstream.net
'Thanks to various people for assistance including
' Anthony DiMauro for work on 3.0x font support
' ...and many others
Private Structure CodeList
Dim Code As String
Dim Status As String
'P=Pending;A=Active;G=Paragraph;D=Dead;K=Killed
'"Dead" means the code is active but will be killed at next
text
'"Pending" means it's waiting for text - if the code is
canceled before text appears it will be killed
'"Active" means there is text using the code at this moment
'"Paragraph" means that the code stays active until the next
paragraph: "/pard" or "/pntext"
End Structure
Private strCurPhrase As String
Dim strHTML As String
Private Codes() As CodeList
Private CodesBeg() As CodeList 'beginning codes
Private NextCodes() As String
Private NextCodesBeg() As String 'beginning codes for next
text
Dim CodesTmp() As String 'temp stack for copying
Dim CodesTmpBeg() As String 'temp stack for copying beg
Private strCR As String 'string to use for CRs - blank if +CR
not chosen in options
Dim strBeforeText As String
Dim strBeforeText2 As String
Dim strBeforeText3 As String
Dim gPlain As Boolean 'true if all codes shouls be popped
before next text
Dim gWBPlain As Boolean 'plain will be true after next text
Dim strColorTable() As String 'table of colors
Dim lColors As Integer '# of colors
Dim strEOL As String 'string to include before <br>
Dim strBOL As String 'string to include after <br>
Dim lSkipWords As Integer 'number od words to skip from
current
Dim gBOL As Boolean 'a <br> was inserted but no non-whitespace
text has been inserted
Dim gPar As Boolean 'true if paragraph was reached since last
text
Dim lBrLev As Integer 'bracket level when finding matching
brackets
Dim strSecTmp As String 'temporary section buffer
Dim gIgnorePard As Boolean 'should pard end list items or not?
Dim strFontTable() As String 'table of fonts
Dim lFonts As Integer '# of fonts
Dim strFont As String
Dim strTable As String
Dim strFace As String 'current font face for setting up
fontstring
Dim strFontColor As String 'current font color for setting up
fontstring
Dim strFontSize As String 'current font size for setting up
fontstring
Dim lFontSize As Integer
Dim iDefFontSize As Short 'default font size
Dim gUseFontFace As Boolean 'use different fonts or always use
default font
Private gDebug As Boolean 'for debugging
Private gStep As Boolean 'for debugging
Private Sub ClearCodes()
ReDim Codes(0)
ReDim CodesBeg(0)
ClearNext()
End Sub
Private Sub ClearNext(Optional ByRef strExcept As String = "")
Dim l As Integer
If Len(strExcept) > 0 Then
If InNext(strExcept) Then
While NextCodes(1) <> strExcept
ShiftNext()
ShiftNextBeg()
End While
Return
End If
End If
ReDim NextCodes(0)
ReDim NextCodesBeg(0)
End Sub
Private Sub ClearFont()
strFont = ""
strTable = ""
strFontColor = ""
strFace = ""
strFontSize = ""
lFontSize = 0
End Sub
Private Sub Codes2NextTill(ByRef strCode As String)
Dim strTmp As String
Dim strTmpbeg As String
Dim l As Integer
For l = 1 To UBound(Codes)
If Codes(l).Code = strCode Then Exit For
If Codes(l).Status <> "K" And Codes(l).Status <> "D" Then
If Not InNext(strCode) Then
UnShiftNext(Codes(l).Code)
UnShiftNextBeg(CodesBeg(l).Code)
End If
End If
Next l
End Sub
Private Sub GetColorTable(ByRef strSecTmp As String, ByRef
strColorTable() As String)
'get color table data and fill in strColorTable array
Dim lColors As Integer
Dim lBOS As Integer
Dim lEOS As Integer
Dim strTmp As String
lBOS = InStr(strSecTmp, "\colortbl")
ReDim strColorTable(0)
lColors = 1
If lBOS <> 0 Then
lBOS = InStr(lBOS, strSecTmp, ";")
lEOS = InStr(lBOS, strSecTmp, ";}")
If lEOS <> 0 Then
lBOS = InStr(lBOS, strSecTmp, "\red")
While ((lBOS <= lEOS) And (lBOS <> 0))
ReDim Preserve strColorTable(lColors)
strTmp = Trim(Hex(CInt(Mid(strSecTmp, lBOS + 4, 1) & _
Convert.ToString(IIf(IsNumeric(Mid(strSecTmp, lBOS +
5, 1)), Mid(strSecTmp, lBOS + 5, 1), "")) & _
Convert.ToString(IIf(IsNumeric(Mid(strSecTmp, lBOS +
6, 1)), Mid(strSecTmp, lBOS + 6, 1), "")))))
If Len(strTmp) = 1 Then strTmp = "0" & strTmp
strColorTable(lColors) = strColorTable(lColors) &
strTmp
lBOS = InStr(lBOS, strSecTmp, "\green")
strTmp = Trim(Hex(CInt(Mid(strSecTmp, lBOS + 6, 1) & _
Convert.ToString(IIf(IsNumeric(Mid(strSecTmp, lBOS +
7, 1)), Mid(strSecTmp, lBOS + 7, 1), "")) & _
Convert.ToString(IIf(IsNumeric(Mid(strSecTmp, lBOS +
8, 1)), Mid(strSecTmp, lBOS + 8, 1), "")))))
If Len(strTmp) = 1 Then strTmp = "0" & strTmp
strColorTable(lColors) = strColorTable(lColors) &
strTmp
lBOS = InStr(lBOS, strSecTmp, "\blue")
strTmp = Trim(Hex(CInt(Mid(strSecTmp, lBOS + 5, 1) & _
Convert.ToString(IIf(IsNumeric(Mid(strSecTmp, lBOS +
6, 1)), Mid(strSecTmp, lBOS + 6, 1), "")) & _
Convert.ToString(IIf(IsNumeric(Mid(strSecTmp, lBOS +
7, 1)), Mid(strSecTmp, lBOS + 7, 1), "")))))
If Len(strTmp) = 1 Then strTmp = "0" & strTmp
strColorTable(lColors) = strColorTable(lColors) &
strTmp
lBOS = InStr(lBOS, strSecTmp, "\red")
lColors = lColors + 1
End While
End If
End If
End Sub
Private Sub GetFontTable(ByRef strSecTmp As String, ByRef
strFontTable() As String)
'get font table data and fill in strFontTable array
Dim lFonts As Integer
Dim lBOS As Integer
Dim lEOS As Integer
Dim strTmp As String
Dim lLvl As Integer
Dim strNextChar As String
lBOS = InStr(strSecTmp, "\fonttbl")
ReDim strFontTable(0)
lFonts = 0
If lBOS <> 0 Then
lEOS = InStr(lBOS, strSecTmp, ";}}")
If lEOS <> 0 Then
lBOS = InStr(lBOS, strSecTmp, "\f0")