c# - 如何从 RichTextBox 获取 RTF

标签 c# .net wpf wpf-controls richtextbox

如何获取 RichTextBox 的 RTF 文本?我正在尝试这样做,但该属性不存在。

RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;

最佳答案

获取用户在 RichTextBox 内部创建的实际 XAML:

   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());

编辑:我面前没有要测试的代码,但是 TextRange 类型的实例有一个 Save (流)方法,它需要一个DataFormats参数,可以是DataFormats.Rtf

关于c# - 如何从 RichTextBox 获取 RTF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/601838/

相关文章:

c# - 从 C# 打印标签 - 文档尺寸对于打印机来说太大

c# - HTML5 Canvas toDataURL ("image/png") 适用于一些绘画,有些则不行,不知道为什么

c# - 什么时候应该使用 LINQ for C#?

wpf - 如何自动拉伸(stretch) ListView 的一列以填充 WPF 中的包含网格?

c# - WPF XAML 解析错误。 "Data at the root level is invalid."

wpf - 如何将多个绑定(bind)添加到 WPF 中的相同依赖属性?

c# - 反序列化二进制文件的一部分

c# - 将 'hd' 参数附加到 redirectUrl ASP.NET Core 1 with Identity 3

c# - EF LINQ ToList 非常慢

c# - 如何直接从xml获取值(最好使用XPath)?