c# - Windows 窗体富文本框。如何在“rtf”中查找粗体、下划线和其他文本格式并为 3rd 方供应商插入其他标签
问题描述
谢谢阅读。
我将 RTF 文本存储在一个数据库列中,该列可以包含 Windows 窗体 Richtextbox 中的粗体、斜体、下划线和其他文本格式。我们需要将文本导出到第三方供应商,该供应商具有一组不同的定义标签,告诉供应商如何格式化文本。不幸的是,第 3 方供应商无法读取 RTF 格式,也无法读取 HTML、XML 等。
我们需要在 Richtextbox 中导出“纯”文本,但要找到用户在文本中添加粗体、斜体、下划线和其他格式的位置,并替换为 3rd 方供应商标签。
例如...我需要解析下面的rtf之类的东西...
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Verdana;}} \viewkind4\uc1\pard\f0\fs18 这是\b粗体\par \par \b0这是\b \b0\i 斜体\par \par \i0 这是\ul 下划线\ulnone\par }
并创建如下内容:
“这是[B]粗体[B]
这是[I]斜体[I]
这是[U]下划线[U]"
有谁知道如何做到这一点?
非常感谢任何帮助。
解决方案
感谢您的所有评论。虽然效率不高也不优雅,但这正是我所需要的。再次感谢大家。
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
推荐阅读
- javascript - 如何使用 Bootstrap modals 和 jQuery 实现可重用的确认对话框?
- visual-studio-code - 为什么 Neovim 不突出显示整个语法?
- javascript - 使用拆分函数创建动态函数
- apache-spark - 如何在 pyspark 上安装 LZO 和 BROTLI 等各种压缩编解码器?
- amazon-web-services - Terraform 中的条件集参数
- sql - 如何在没有公共 ID 的情况下返回另一个表中尚不存在的行
- vue.js - 使用 Vue 和 Cypress 使用 Slots 测试组件
- php - 如何将变量名称链接到活动日志描述中?拉拉维尔 8
- java - 如何每 x 米在多边形内创建标记
- firebase - 请求被中止,因为没有可用的实例