Get plain text from an RTF text

21,596

Solution 1

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

Solution 2

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;

    }
Share:
21,596
rpf
Author by

rpf

Hi everyone.... My name is rpf and I'm from Portugal, city of Oporto. I'm a software engineer. On a professional level I work with C # and MSSQL Server. For me the best programming languages are those who are object oriented...... Besides these, I love C :) To finhish, I'm a lover of all kind of tecnology.

Updated on July 09, 2022

Comments

  • rpf
    rpf almost 2 years

    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