从RTF文本中获取纯文本

我的数据库中有一个包含RTF格式文本的列。

我怎样才能使用C#获得它的纯文本?

感谢:D

微软提供了一个例子 ,他们基本上将rtf文本粘贴在RichTextBox ,然后读取.Text属性……感觉有些笨拙,但它有效。

 static public string ConvertToText(string rtf) { using(RichTextBox rtb = new RichTextBox()) { rtb.Rtf = rtf; return rtb.Text; } } 

如果你想要一个纯代码版本,你可以自己解析rtf并只保留文本位。 这是一项工作,但不是很困难的工作 – RTF文件的语法非常简单。 在RTF规范中阅读它 。

对于WPF,您可以使用(使用Xceed WPF Toolkit)这种扩展方法:

 public static string RTFToPlainText(this string s) { // for information : default Xceed.Wpf.Toolkit.RichTextBox formatter is RtfFormatter Xceed.Wpf.Toolkit.RichTextBox rtBox = new Xceed.Wpf.Toolkit.RichTextBox(new System.Windows.Documents.FlowDocument()); rtBox.Text = s; rtBox.TextFormatter = new Xceed.Wpf.Toolkit.PlainTextFormatter(); return rtBox.Text; }