我希望获取一些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您是否知道如何使用正则表达式或其他方法安全地清理RTF?我使用VB.NET进行处理,但任何.NET语言示例都可以。
发布于 2008-08-21 17:39:41
我将使用一个隐藏的RichTextBox,设置Rtf成员,然后检索文本成员,以一种支持良好的方式清理RTF。然后,我将使用手动注入所需的格式。
发布于 2008-08-21 18:22:48
我会做类似如下的事情:
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)发布于 2008-08-21 16:51:54
你可以用正则表达式去掉标签。只需确保您的表达式不会过滤实际上是文本的标记。如果文本的正文中有" \b“,它将在RTF流中显示为\b。换句话说,您将匹配"\b“而不是"\b”。
您可能会走捷径,过滤掉标题RTF标记。查找输入中第一个出现的"\viewkind4“。然后预读到第一个空格字符。您将删除从文本开头到该空格字符为止的所有字符。这将剥离RTF标题信息(字体、颜色等)。
https://stackoverflow.com/questions/20450
复制相似问题