How to get RTF from RichTextBox

て烟熏妆下的殇ゞ 提交于 2019-11-28 08:36:21

To get the actual XAML created by the user inside of the RichTextBox:

   TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
                                myRichTextBox.Document.ContentEnd);
   MemoryStream ms = new MemoryStream();
   tr.Save(ms, DataFormats.Xaml);
   string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());

EDIT: I don't have code in front of me to test, but an instance of the TextRange type has a Save (to stream) method that takes a DataFormats parameter, which can be DataFormats.Rtf

There are 2 RichTextBox classes, one from the winforms framework and one from the WPF framework:

System.Windows.Controls.RichTextBox wpfBox;
System.Windows.Forms.RichTextBox winformsBox;

Only the Winforms RichTextBox has an Rtf property, the other has a Document property which contains a FlowDocument.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!