# .NET and More > WPF, WCF, WF >  [RESOLVED] TextRange does not get RTF of WPF RichTextbox

## dee-u

I am trying to use the following code but it seems that TextRange only returns the 'plain' text from WPF Richtextbox, it does not return the 'rtf' of the control unlike in Winforms, how can I then get the rtf of the Richtextbox control?



```
TextRange textRange = new TextRange(RTB.Document.ContentStart, RTB.Document.ContentEnd  );
MessageBox.Show(textRange.Text);
```

----------


## jmcilhinney

According to the MSDN documentation for the TextRange class, the Text property:


> Gets or sets the *plain text contents* of the current selection.


According to Ged Mead's blog, you can call Save on that TextRange and specify Rtf as the DataFormat.  If you wanted to actually see the raw RTF markup then you could save to a MemoryStream and read that with a StreamReader.  Presumably what you're actually trying to achieve is to save it to a file though, so you'd want to save to a FileStream, just as Ged demonstrates.

I should point out that I have never once used the WPF RichTextBox control or the TextRange class.  I simply spent a few minutes searching and reading.

----------


## dee-u

Well, I need to be able to save to a database its RTF hence I am trying to extract it without saving first. Will try the MemoryStream method and see how it goes. Why on earth did Microsoft make it so difficult to get the WPF's RichTextbox compared to Winforms?

EDIT:
Haven't tried it but found the following code from here.



```
public string GetRTF()
        {
            TextRange range = new TextRange(RichTextControl.Document.ContentStart, 
                RichTextControl.Document.ContentEnd);

            // Exception abfangen für StreamReader und MemoryStream
            try
            {
                using (MemoryStream rtfMemoryStream = new MemoryStream())
                {
                    using (StreamWriter rtfStreamWriter = 
                                             new StreamWriter(rtfMemoryStream))
                    {
                        range.Save(rtfMemoryStream, DataFormats.Rtf);

                        rtfMemoryStream.Flush();
                        rtfMemoryStream.Position = 0;
                        StreamReader sr = new StreamReader(rtfMemoryStream);
                        return sr.ReadToEnd();
                    }
                }
            }
            catch (Exception)
            {
                throw;                
            }
        }
```

----------


## jmcilhinney

> Why on earth did Microsoft make it so difficult to get the WPF's RichTextbox compared to Winforms?


Because they wanted the RichTextBox to follow the same rules as other document-centric controls.  Why not create an extension method?

```
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

public static class RichTextBoxExtensions
{
    public static string GetRtf(this RichTextBox source)
    {
        var document = source.Document;
        var range = new TextRange(document.ContentStart, document.ContentEnd);

        using (var stream = new MemoryStream())
        using (var reader = new StreamReader(stream))
        {
            range.Save(stream, DataFormats.Rtf);

            return reader.ReadToEnd();
        }
    }
}
```

You write it once, compile it into a DLL and now it's just as easy to get the RTF markup from a WPF RTB as it is for a WinForms RTB.  Just note that that code is untested and you may have to set the Position or call Seek on the MemoryStream so that it gets read from the start and not the end.

----------

