清理RTF文本

Vin*_*ent 10 .net vb.net rtf ms-word

我想取一些RTF输入并清除它以删除除\ ul\b\i之外的所有RTF格式,以便将其粘贴到带有次要格式信息的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
Run Code Online (Sandbox Code Playgroud)

您是否知道如何使用正则表达式或其他东西安全地清理RTF?我使用VB.NET进行处理,但任何.NET语言示例都可以.

Nic*_*ick 6

我将使用隐藏的RichTextBox,设置Rtf成员,然后检索Text成员以便以良好支持的方式清理RTF.然后我会使用之后手动注入所需的格式.


Mar*_*tin 5

我会做类似以下的事情:

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)
Run Code Online (Sandbox Code Playgroud)