Use the Text property of the TextRange instead of .ToString()
A way to get the contents of a RichTextBox string as:
public static string GetStringFromRichTextBox(RichTextBox richTextBox) { TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); return textRange.Text; }
A way to get RichTextBox content as rich text:
public static string GetRtfStringFromRichTextBox(RichTextBox richTextBox) { TextRange textRange = new TextRange(richTextBox.Document.ContentStart, richTextBox.Document.ContentEnd); MemoryStream ms = new MemoryStream(); textRange.Save(ms, DataFormats.Rtf); return Encoding.Default.GetString(ms.ToArray()); }
Edit: You can put rich text returned from GetRtfStringFromRichTextBox () in another RichText control by doing the following:
FlowDocument fd = new FlowDocument(); MemoryStream ms = new MemoryStream(Encoding.ASCII.GetBytes(richTextString)); TextRange textRange = new TextRange(fd.ContentStart, fd.ContentEnd); textRange.Load(ms, DataFormats.Rtf); richTextBox.Document = fd;
source share