tags:

views:

1015

answers:

3

Hello

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

+9  A: 

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)
{
   RichTextBox rtb = new RichTextBox();
   rtb.Rtf = rtf;
   return rtb.Text;
}
Daniel LeCheminant
This always annoyed me. Plus, you have to do this in a STA thread, which usually messes with most program's threading model.
Will
@Will: I wasn't claiming it was the best way...
Daniel LeCheminant
Having looked at the underlying RichTextBox code... yeah, you're going to want to use it because it's a complex beast.
Orion Adrian
+1  A: 

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
yeah, until you get to tables with columns :) But simple bold/italic formatting is easy.
crashmstr
Good point. Devil's in the details.
Frank Krueger
+1  A: 

Here's another question that discusses the regex way.

le dorfier