Results 1 to 4 of 4

Thread: [RESOLVED] TextRange does not get RTF of WPF RichTextbox

  1. #1

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Resolved [RESOLVED] TextRange does not get RTF of WPF RichTextbox

    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?

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


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: TextRange does not get RTF of WPF RichTextbox

    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.

  3. #3

    Thread Starter
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: TextRange does not get RTF of WPF RichTextbox

    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.

    Code:
    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;                
                }
            }
    Last edited by dee-u; Apr 26th, 2013 at 12:45 AM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: TextRange does not get RTF of WPF RichTextbox

    Quote Originally Posted by dee-u View Post
    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?
    Code:
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width