rpf
rpf

Reputation: 3732

Get plain text from an RTF text

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

Upvotes: 20

Views: 24340

Answers (3)

Xavave
Xavave

Reputation: 695

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;

    }

Upvotes: 1

Daniel LeCheminant
Daniel LeCheminant

Reputation: 51131

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;
   }
}

Upvotes: 34

Frank Krueger
Frank Krueger

Reputation: 71053

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.

Upvotes: 0

Related Questions