C# 如何从 RichTextBox 获取 RTF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/601838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get RTF from RichTextBox
提问by rpf
How do I get the text in RTF of a RichTextBox
? I'm trying to get like this, but the property does not exist.
如何获取 a 的 RTF 格式的文本RichTextBox
?我正在尝试这样做,但该属性不存在。
RichTextBox rtb = new RichTextBox();
string s = rtb.Rtf;
采纳答案by Mitch Wheat
To get the actual XAML created by the user inside of the RichTextBox:
要获取用户在 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());
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
编辑:我面前没有要测试的代码,但是该TextRange
类型的实例有一个Save
(流)方法,它接受一个DataFormats
参数,可以是DataFormats.Rtf
回答by Wouter
There are 2 RichTextBox classes, one from the winforms framework and one from the WPF framework:
有 2 个 RichTextBox 类,一个来自 winforms 框架,一个来自 WPF 框架:
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.
只有 Winforms RichTextBox 具有 Rtf 属性,另一个具有包含 FlowDocument 的 Document 属性。