19

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

Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
rpf
  • 3,612
  • 10
  • 38
  • 47
  • [Here's another question](http://stackoverflow.com/questions/188545/regular-expression-for-extracting-text-from-an-rtf-string) that discusses the regex way. – dkretz Feb 27 '09 at 18:09

3 Answers3

33

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;
   }
}
wal
  • 17,409
  • 8
  • 74
  • 109
Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
  • 2
    This always annoyed me. Plus, you have to do this in a STA thread, which usually messes with most program's threading model. –  Feb 27 '09 at 18:07
  • Having looked at the underlying RichTextBox code... yeah, you're going to want to use it because it's a complex beast. – Orion Adrian Feb 27 '09 at 18:36
  • i have had issues with this creating a memory leak. Even tho the `RichTextBox rtb` goes out of scope immediately it appears to add to the USER OBJECTs count and never decrement. thus I think it best to wrap it in a using statement. – wal Apr 04 '13 at 23:11
  • 2
    Please note that if your RTF inclucludes headers and footers, that text won't be captured by RichTextBox component. – Gonzalo Méndez May 21 '14 at 18:43
1

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;

    }
Xavave
  • 645
  • 11
  • 15
0

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.

Frank Krueger
  • 69,552
  • 46
  • 163
  • 208