清除RTF文字
时间:2020-03-05 18:41:38 来源:igfitidea点击:
我想接受一些RTF输入并将其清除,以删除所有RTF格式,但\ ul \ b \ i会将其粘贴到具有次要格式信息的Word中。
用于粘贴到Word中的命令将类似于:
oWord.ActiveDocument.ActiveWindow.Selection.PasteAndFormat(0)(剪贴板中已有一些RTF文本)
{\rtf1\ansi\deff0{\fonttbl{\f0\fnil\fcharset0 Courier New;}} {\colortbl ;\red255\green255\blue140;} \viewkind4\uc1\pard\highlight1\lang3084\f0\fs18 The company is a global leader in responsible tourism and was \ul the first major hotel chain in North America\ulnone to embrace environmental stewardship within its daily operations\highlight0\par
我们对我如何使用一些正则表达式或者其他东西安全地清除RTF有任何想法吗?我正在使用VB.NET进行处理,但是任何.NET语言示例都可以。
解决方案
回答
我们可以使用正则表达式去除标签。只要确保表达式不会过滤实际上是文本的标签即可。如果文本的正文中带有" \ b",则它将在RTF流中显示为\ b。换句话说,我们将在" \ b"上匹配,但在" \ b"上不匹配。
我们可能会采取捷径并过滤掉标题RTF标签。在输入中查找" \ viewkind4"的第一个匹配项。然后,先阅读第一个空格字符。我们将删除从文本开头到该空格字符(包括该空格字符)的所有字符。这样可以去除RTF标头信息(字体,颜色等)。
回答
我将使用隐藏的RichTextBox,设置Rtf成员,然后检索Text成员以一种受支持的方式对RTF进行清理。然后,我将使用手动注入所需的格式。
回答
我会做如下的事情:
Dim unformatedtext As String someRTFtext = Replace(someRTFtext, "\ul", "[ul]") someRTFtext = Replace(someRTFtext, "\b", "[b]") someRTFtext = Replace(someRTFtext, "\i", "[i]") Dim RTFConvert As RichTextBox = New RichTextBox RTFConvert.Rtf = someRTFtext unformatedtext = RTFConvert.Text unformatedtext = Replace(unformatedtext, "[ul]", "\ul") unformatedtext = Replace(unformatedtext, "[b]", "\b") unformatedtext = Replace(unformatedtext, "[i]", "\i") Clipboard.SetText(unformatedtext) oWord.ActiveDocument.ActiveWindow.Selection.PasteAndFormat(0)