从 RTF 文本中获取纯文本

发布于 2024-07-13 21:46:52 字数 76 浏览 6 评论 0原文

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

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

感谢:D

I have on my database a column that holds text in RTF format.

How can I get only the plain text of it, using C#?

Thanks :D

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

壹場煙雨 2024-07-20 21:46:52

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

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

Microsoft provides an example where they basically stick the rtf text in a RichTextBox and then read the .Text property... it feels somewhat kludgy, but it works.

static public string ConvertToText(string rtf)
{
   using(RichTextBox rtb = new RichTextBox())
   {
       rtb.Rtf = rtf;
       return rtb.Text;
   }
}
音盲 2024-07-20 21:46:52

对于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;

    }

for WPF you can use
(using Xceed WPF Toolkit) this extension method :

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;

    }
桃扇骨 2024-07-20 21:46:52

如果您想要纯代码版本,您可以自己解析 rtf 并仅保留文本位。 这需要一些工作,但并不是非常困难的工作 - RTF 文件的语法非常简单。 在 RTF 规范中了解相关内容

If you want a pure code version, you can parse the rtf yourself and keep only the text bits. It's a bit of work, but not very difficult work - RTF files have a very simple syntax. Read about it in the RTF spec.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文