Thanks for reading.
I am storing RTF text in a database column that can contain bold, italics, underline, and other text formatting from a Windows form Richtextbox. We need to export the text to a 3rd party vendor that has a different defined set of tags that tells the vendor how to format the text. Unfortunately, the 3rd party vendor cannot read RTF format nor can they read HTML, XML, etc.
We need to export the 'plain' text in the richtextbox but find where the user added bold, italic, underline, and other formatting to the text and replace with the 3rd party vendor tags.
For example... I need to parse through something like the rtf below...
{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Verdana;}} \\viewkind4\\uc1\\pard\\f0\\fs18 This is \\b bold\\par \\par \\b0 This is\\b \\b0\\i italics\\par \\par \\i0 This is \\ul underline\\ulnone\\par }
and create something like this:
"this is [B]bold[B]
this is [I]italics[I]
this is [U]underline[U]"
Does anyone know how this could be accomplished?
Greatly appreciate any help.
Thanks for all of your comments. Although not very efficient nor elegant this is getting what I need. Thank you all again.
Public Shared Function ParseRTF(ByRef rtf As RichTextBox) As String
Dim returnText As String = ""
Dim isBold As Boolean = False
Dim isUnderline As Boolean = False
Dim isItalics As Boolean = False
Dim isBullets As Boolean = False
Dim isBoldBegin As Boolean = False
Dim isUnderlineBegin As Boolean = False
Dim isItalicsBegin As Boolean = False
Dim isBulletsBegin As Boolean = False
Dim isBoldEnd As Boolean = False
Dim isUnderlineEnd As Boolean = False
Dim isItalicsEnd As Boolean = False
Dim isBulletsEnd As Boolean = False
Dim tags As String = String.Empty
Dim bold As String = "<B>"
Dim underline As String = "<U>"
Dim italics As String = "<I>"
Dim bullet As String = "<T2><BULLET>"
Dim newLine As String = "<NL>"
Dim lastChar As String = String.Empty
rtf.SelectionStart = 0
rtf.SelectionLength = 0
Try
For x As Integer = 0 To rtf.TextLength - 1
rtf.SelectionStart = x
rtf.SelectionLength = 1
tags = String.Empty
isBoldBegin = False
isBoldEnd = False
isUnderlineBegin = False
isUnderlineEnd = False
isItalicsBegin = False
isItalicsEnd = False
'Test if text has formatting
If rtf.SelectionFont.Bold Then
If Not isBold Then
isBoldBegin = True
isBold = True
isBoldEnd = False
End If
Else
If isBold Then
isBoldBegin = False
isBold = False
isBoldEnd = True
End If
End If
If rtf.SelectionFont.Italic Then
If Not isItalics Then
isItalicsBegin = True
isItalics = True
isItalicsEnd = False
End If
Else
If isItalics Then
isItalicsBegin = False
isItalics = False
isItalicsEnd = True
End If
End If
If rtf.SelectionFont.Underline Then
If Not isUnderline Then
isUnderlineBegin = True
isUnderline = True
isUnderlineEnd = False
End If
Else
If isUnderline Then
isUnderlineBegin = False
isUnderline = False
isUnderlineEnd = True
End If
End If
'END Test if text has formatting
' Do reverse order if for any "END" tags
If isUnderlineEnd Then
tags += underline
End If
If isItalicsEnd Then
tags += italics
End If
If isBoldEnd Then
tags += bold
End If
'Work with beginning tags
If isBoldBegin Then
tags += bold
End If
If isItalicsBegin Then
tags += italics
End If
If isUnderlineBegin Then
tags += underline
End If
If x = rtf.TextLength - 1 Then
If isUnderline Then
lastChar += underline
End If
If isItalics Then
lastChar += italics
End If
If isBold Then
lastChar += bold
End If
End If
If rtf.SelectedText.Contains(vbLf) Then
If Not String.IsNullOrWhiteSpace(tags) Then
If isBoldBegin Or isUnderlineBegin Or isItalicsBegin Then
returnText += newLine & tags
End If
If isBoldEnd Or isUnderlineEnd Or isItalicsEnd Then
returnText += tags & newLine
End If
Else
returnText += newLine
End If
Else
returnText += tags & rtf.SelectedText
End If
If Not String.IsNullOrWhiteSpace(lastChar) Then
returnText += lastChar
End If
Next
Return returnText
Catch ex As Exception
Throw New CvrException(ex, ErrorCodes.UNKNOWN_ERROR)
Finally
End Try
End Function
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.