Library code snippets
Convert RTF to HTML
By Brady H, published on 14 Jul 2001
Visual Basic module which changes text from RTF to HTML. Note that it requires an entire RTF document including header codes, however the text it outputs does not include the HTML header codes. This is a feature not a bug since the header codes are much easier to add than to remove.
The program currently supports the following:
- Most if not all ascii characters are converted from RTF codes to HTML codes
- Bold, Italicized, Strikeout and Underlined text
- Bulleted lists
- Colored fonts
- Fonts
- Font Size
- Centered and Right justified text
- Produces clean and correct HTML
- Other stuff...
Use the following function in the module to perform the conversion:
Function rtf2html3(strRTF As String, Optional strOptions As String) As String
More information can be found here
Related articles
Related discussion
-
VB6, SQL 2005 & DMO
by elajaunie3 (1 replies)
-
sending sms from pc
by sriraj20074 (0 replies)
-
Automating Excel from VB6.0
by epurdy (0 replies)
-
VB6 system conversion using VBA to Word 2007
by b.macgregor@vodamail.co.za (0 replies)
-
video not working with visual basic
by Jupiter 2 (9 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
i am really confused on how to call this function. I have a .rtf file lets say and i dont want any of the options so i just want to call this function out of my main .asp page and then have the .rtf be converted to html. Please help sorry for being a noob but i just started using VB
thanks in advance
kenny
Hi Dears
I am developing in VB6. I want to take any print from Data Report to Dot Matrix Printer. I using Data Report (VB6) not any Crystal Reports. If it is possible to take any print to Dot Matrix Printer from Data Report (VB6) so please help me as soon as possible and please write me how to solve this porblem by source codes.
Thanking you,
Partha Chatterjee
India
impartha@hotmail.com
partha@chitajute.com
Can this convert HTML code to RTF code?
Apologies - it seems in the conversion from source code to HTML on this website, a few errors were introduced. We're now hosting a Zip file with the source code instead.
I tried a number of RTF files against this and this code seems to cut chunks of the actual text off!!
I found another RTF -> HTML convert at www.easybyte.com, it seems to do the job fine.
wow what code! I'm not the author but by looking at the params i would think its rtf2html3!
Function rtf2html3(strRTF As String, Optional strOptions As String) As String
so youd do rtf2html3 <richtextbox>.rtftext, <options>
the options are:
'Options:
'+H add an HTML header and footer
'+G add a generator Metatag
'+T="MyTitle" add a title (only works if +H is used)
'+CR add a carraige return after all <br>s
'+I keep html codes intact
'+F=X default font size (blanks out any changes to this size - saves on space)
'-FF ignore font faces
does that hlep?
How do I call these functions?Which one is the main function?
Thanks
This code is almost exactly what I've been looking for.
I've run into a couple small problems though.
1. Why does it put everything in tables?
2. It seems to be adding an extra <br> between each line.
Ex:
This is a test
This is only a test
Displays like:
This is a test
This is only a test
If you could help me fix those problems I would be eternally gratefull!
As far as Gustav's post about Easybyte. They're "Free" converter is nagware and they want $400 dollars for their dll with out the nag and $700 for the source code. I MIGHT have considered paying $10 or $20, but not a penny more, especially after looking at their pathetic excuse for a web site.
Hi,
I cann't Convert this Code:
"{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fnil\fcharset2 Tawfiq;}}
\viewkind4\uc1\pard\f0\fs41\'b4\'c0\'a5\'c3\'b7\'a5\'ba
\par }
Thanks
Samurai
How can I convert html file from charset window-1252 to UTF-8?
You can help me with dll
Thanks
Special characters are not recognize !!
for example: à é '
The function HTMLCode seems to be the one which do the job but it 's not called.
What the matter!
Try This One...
Here is the code which converts RTF fromat to XML
Include the following references from Project Menu
1. Microsoft Word 8.0 Object Library / I have Microsoft Word 10.0 Object Library
2. Microsoft Scripting Runtime
Insert component from Project Menu
1. Microsoft Internet Controls
Draw an instance of Web Browser
Paste this code : Under Command button Command1
Private Sub Command1_Click()
Dim wordApp As Application
Dim Doc As Document
Dim fso As New FileSystemObject
Dim txtStream As TextStream
Set wordApp = CreateObject("Word.Application")
Set txtStream = fso.CreateTextFile(App.Path & "\abc.rtf", True, False)
txtStream.Write (RichTextBox1.TextRTF)
txtStream.Close
Set Doc = wordApp.Documents.Open(App.Path & "\abc.rtf")
Doc.SaveAs App.Path & "\abc.html", wdFormatHTML
Doc.Close
Set Doc = Nothing
WebBrowser1.Navigate (App.Path & "\abc1.html")
End Sub
Type some text in Rtf and click on Command 1
i'll give you a tip in html character codes (well, if you like to extend it...) , why don't you try to look for the html codes in some sites (example, your code in your "select case" statement inside HTMLCode) and try to place it in a "for - next" statement. just get the sequence of the bytecode and try to make a formula to make it more faster and easier for the compiler... even though its my first time to register here in developer fusion, i've got a lot of experiences in programming languages: HTML, ASP, Javascript, VB, C++ (native one), SQL... but at least i tried to help you optimize your code. any help you need from me or tips to give to me, just e-mail me here at eight_bytes@yahoo.com
This piece of code goes into a never ending loop
While Len(strRTFTmp) > 0
strSecTmp = NabNextLine(strRTFTmp)
While Len(strSecTmp) > 0
strWordTmp = NabNextWord(strSecTmp)
If Len(strWordTmp) > 0 Then ProcessWord(strWordTmp)
End While
End While
I am using VB.Net has nayone got any suggestions?
I found this one (in the proc rtf2html)
Original
lBOS = InStr(strRTFTmp, "\colortbl")
If lBOS > 0 Then
strSecTmp = NabSection(strRTFTmp, lBOS)
GetColorTable strSecTmp, strColorTable()
End If
This works
lBOS = InStr(strRTFTmp, "\colortbl")
If lBOS > 0 Then
strSecTmp = NabSection(strRTFTmp, lBOS)
GetColorTable(strSecTmp, strColorTable)
End If
The modified function (GetColorTable) has an error with array construction. This will not work under Windows 95. (crash with an error : Runtime error 9, subscript out of range). Though it works well under 2000/XP.
Hey,
I tried to use your code, but am quite new to asp and got this error:
Runtimeerror Microsoft VBScript (0x800A000D)
Types do not match: 'rtf2html'
I try to implement it like this:
<%=rtf2html("blaat", "+H")%>
and I put the code in rtf2html.inc wich I include on top of the page I load...
I am making a stupid mistake so please correct me
Greetz,
Rutger
Although the colour section worked perfiectly well, it was a bit messy. Here is a reword of that function:
Function GetColorTable(strSecTmp As String, strColorTable() As String)
Dim i As Integer
'get font table data and fill in strFontTable array
'We can turn the whole thing into an aray, based of ";", where we ignore the first and last item.
ReDim strColorTable(CInt(UBound(Split(strSecTmp, ";")) - 2)) As String
For i = 1 To UBound(Split(strSecTmp, ";")) - 1
'We only want the section from the first space to the end....using ";}{" as an array splitter
strColorTable(i - 1) = GetColor(CStr(Split(strSecTmp, ";")(i)))
Next i
End Function
Function GetColor(ByVal ColourSection As String) As String
Dim strRed As String
Dim strGreen As String
Dim strBlue As String
strRed = Hex(CByte(Right(Split(ColourSection, "\")(1), Len(Split(ColourSection, "\")(1)) - 3)))
If Len(strRed) = 1 Then strRed = "0" & strRed
strGreen = Hex(CByte(Right(Split(ColourSection, "\")(2), Len(Split(ColourSection, "\")(2)) - 5)))
If Len(strGreen) = 1 Then strGreen = "0" & strGreen
strBlue = Hex(CByte(Right(Split(ColourSection, "\")(3), Len(Split(ColourSection, "\")(3)) - 4)))
If Len(strBlue) = 1 Then strBlue = "0" & strBlue
GetColor = "#" & strRed & strGreen & strBlue
End Function
There were, in fact, at least [5]35 [/5] unused variables!!!!!
Wow.....
Here is a list of variables that are not used:
Modular Level:
'Dim gWBPlain As Boolean 'plain will be true after next text
'Dim lColors As Long '# of colors
'Dim lFonts As Long '# of fonts
'Dim strBOL As String 'string to include after <br>
within RTFtoHTML:
' Dim strHTML As String
' Dim l As Long
' Dim lTmp As Long
' Dim lTmp2 As Long
' Dim lTmp3 As Long
' Dim lRTFLen As Long
' Dim lEOS As Long 'end of section
' Dim strTmp As String
' Dim strTmp2 As String
' Dim strEOS As String 'string to be added to end of section
' Dim strBOS As String 'string to be added to beginning of section
' Dim strEOP As String 'string to be added to end of paragraph
' Dim strBOL As String 'string to be added to the begining of each new line
' Dim strEOL As String 'string to be added to the end of each new line
' Dim strEOLL As String 'string to be added to the end of previous line
' Dim strCurFont As String 'current font code eg: "f3"
' Dim strCurFontSize As String 'current font size eg: "fs20"
' Dim strCurColor As String 'current font color eg: "cf2"
' Dim strFontFace As String 'Font face for current font
' Dim strFontColor As String 'Font color for current font
' Dim lFontSize As Integer 'Font size for current font
' Const gHellFrozenOver = False 'always false
' Dim gSkip As Boolean 'skip to next word/command
' Dim strCodes As String 'codes for ascii to HTML char conversion
' Dim strCurLine As String 'temp storage for text for current line before being added to strHTML
' Dim strFontCodes As String 'list of font code modifiers
' Dim gSeekingText As Boolean 'True if we have to hit text before inserting a </FONT>
' Dim gText As Boolean 'true if there is text (as opposed to a control code) in strTmp
' Dim strAlign As String '"center" or "right"
' Dim gAlign As Boolean 'if current text is aligned
' Dim strGen As String 'Temp store for Generator Meta Tag if requested
' Dim strTitle As String 'Temp store for Title if requested
*NOTE*
I have not checked other functions.
Like I said in my first post, this code is a good start and probably saved me a good day or two.... but it has still taken me a day to port this code to something more stable.
The ProcessWord, /F option - else if replacement should look like this:
ElseIf IsNumeric(Mid(strWord, 3)) Then 'Font Type
strFontFace = strFontTable(Mid(strWord, 3))
If strFontFace <> "" Then
strFont = ParseFont(strFontColor, strFontSize, strFontFace)
If InNext("</font>") Then
ReplaceInNextBeg "</font>", strFont
ElseIf InCodes("</font>") Then
PushNext ("</font>")
PushNextBeg (strFont)
Codes2NextTill "</font>"
Else
PushNext ("</font>")
PushNextBeg (strFont)
End If
End If
End If
If it doesn't then every some fonts will be ignored. Sorry again
My syntax was wrong in the ParseFont Function. It should be:
Function ParseFont(strColor As String, strSize As String, strFont As String) As String
Dim strTmpFont As String
strTmpFont = "<font"
If strColor <> "" Then
strTmpFont = strTmpFont & " color=""" & strColor & """"
End If
If strSize <> "" And strSize <> "2" Then
strTmpFont = strTmpFont & " size=" & strSize
End If
If strFontFace <> "" Then
strTmpFont = strTmpFont & " face=""" & strFont & """"
End If
strTmpFont = strTmpFont & ">"
ParseFont = strTmpFont
End Function
Notice the font face now has double quotation marks.
a more hands-on approach i guess than PSC... its got quality source-code and tutorials and some of the best support poeple around... the main attraction would be i guess the forums!
ummm.... don't mean to grumble but the way the Font type was being constructed appeared buggy. I have rewritten it below (feel free to tell me if I am doing it wrong). Much much smaller and I think it might even be faster.
Function GetFontTable(strSecTmp As String, strFontTable() As String)
Dim i As Integer
'get font table data and fill in strFontTable array
'We already know that this is multiple {} seperated values, and we should be looking
'at font definitions ONLY....
Dim strFonts As String
'Strip off the begining of the section (and the first curly brace and last brace and semi-colon)
strFonts = Mid(strSecTmp, 11, Len(strSecTmp) - 13)
ReDim strFontTable(CInt(UBound(Split(strFonts, ";}{")))) As String
For i = 0 To UBound(Split(strFonts, ";}{"))
'We only want the section from the first space to the end....using ";}{" as an array splitter
strFontTable(i) = Right(Split(strFonts, ";}{")(i), Len(Split(strFonts, ";}{")(i)) - InStr(1, Split(strFonts, ";}{")(i), " "))
Next i
end Function
No problem. I found this sight through Google.... what is it all about? Is it like PlanetSourceCode?
Upon further review.... the code is simply incomplete.
There are numerous (15-20) variables that are declared but not used.
The code loaded a "Font" table, but then just ignored it
The code indicated multiple arguments, of which only two were used.
In the General section:
Dim strFontFace as string
Function ParseFont(strColor As String, strSize As String, strFont As String) As String
Dim strTmpFont As String
strTmpFont = "<font"
If strColor <> "" Then
strTmpFont = strTmpFont & " color=""" & strColor & """"
End If
If strSize <> "" And strSize <> "2" Then
strTmpFont = strTmpFont & " size=" & strSize
End If
If strFontFace <> "" Then
strTmpFont = strTmpFont & " face=" & strFont
End If
strTmpFont = strTmpFont & ">"
ParseFont = strTmpFont
End Function
ALSO in the ProcessWord function alterations need to be made
in the Case "\f" section, remove the main "End IF" (the endif for the first if) and replace it with
ElseIf IsNumeric(Mid(strWord, 3)) Then 'Font Type
strFontFace = strFontTable(Mid(strWord, 3))
End If
ALSO in the ProcessWord function change every call to ParseFont to:
ParseFont(strFontColor, strFontSize, strFontFace)
If InStr(strOptions, "+H") Then
RTF2HTML = "<html><body>" & RTF2HTML & "</body></html>"
End If
Crude, but effective.
good to see more aussies around!
Good - but what about the text Font type, background colours and image support?
Thanks!!
I tried to execute the code as per ur direction... but it gave the following result:
\rtf1
\ansi
\deff0
\viewkind4
\uc1
\pard
\lang1033
\f0
\fs17
Test
\par
This is a test.....
\par
Doono what kind of output it will be!!
\par
Test <br>This is a test.....<br>Doono what kind of output it will be!!
The generated code was not at all displaying the input text as html.
I used to following code for this:
Private Sub Command1_Click()
Debug.Print rtf2html(RichTextBox1.TextRTF, "+H")
End Sub
Private Sub Form_Load()
RichTextBox1.BackColor = &H80000004
RichTextBox1.Font = Verdana
RichTextBox1.TextRTF = "Test"
End Sub
Kindly direct me more to achive the desired result.
In appriciation
Butterfly
You need to call the rtf2html function.
Hi there,
The code displayed here is really commandable. but i couldnt understand which all funtions to be used to execute to convert RTFtext to HTML.
It states to copy the code and paste in a module (.bas) but which all functions need to be executed to convert the code it hasn't mention that.
Please direct me for the same.
Thanks.
Butterfly.
RTF =RichTextFormat ... it is used in the RichTextBox (which allows you to add bold/italic text effects etc). This code lets you convert the bold/italic text in a richtextbox into a HTML page.
Take a look at some of the items in the "related" panel on the right-hand side of the code for some links to related articles.
what is the meaning of RTF?
what is the need to convert it into HTML codes?
thankx in advance.
and implemented in the my simple project.
probably in my project there is an error because
the software run incorrectly.
Please help me.
Thank You very for your kindness.
Ulisse Quadri
email: ulisse.quadri@libero.it
Perfect - thanks for saving me from learning RTF.
An inspired piece of code!
This thread is for discussions of Convert RTF to HTML.