C# 如何将 FlowDocument 转换为 rtf
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/829053/
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 convert FlowDocument to rtf
提问by
I have used a WPF RichTextBox to save a flowdocument from it as byte[] in database. Now i need to retrieve this data and display in a report RichTextBox as an rtf. when i try to convert the byte[] using TextRange or in XAMLReader i get a FlowDocument back but how do i convert it to rtf string as the report RichTextBox only takes rtf.
我使用 WPF RichTextBox 将流文档保存为数据库中的字节 []。现在我需要检索这些数据并在报告 RichTextBox 中显示为 rtf。当我尝试使用 TextRange 或在 XAMLReader 中转换字节 [] 时,我得到了一个 FlowDocument,但我如何将其转换为 rtf 字符串,因为报告 RichTextBox 只需要 rtf。
Thanks
谢谢
Arvind
阿文德
采纳答案by Peter Lillevold
You should not persist the FlowDocument directly as it should be considered the runtime representation of the document, not the actual document content. Instead, use the TextRange classto Save and Load to various formats including Rtf.
您不应直接保留 FlowDocument,因为它应被视为文档的运行时表示,而不是实际的文档内容。相反,使用TextRange 类将保存和加载为各种格式,包括Rtf。
A quick sample on how to create a selection and save to a stream:
关于如何创建选择并保存到流的快速示例:
var content = new TextRange(doc.ContentStart, doc.ContentEnd);
if (content.CanSave(DataFormats.Rtf))
{
using (var stream = new MemoryStream())
{
content.Save(stream, DataFormats.Rtf);
}
}
To load content into a selection would be similar:
将内容加载到选择中将是类似的:
var content = new TextRange(doc.ContentStart, doc.ContentEnd);
if (content.CanLoad(DataFormats.Rtf))
{
content.Load(stream, DataFormats.Rtf);
}
回答by Slampen
This works like a charm for me. Displays the result in an RTF box without difficulties.
这对我来说就像一种魅力。毫无困难地在 RTF 框中显示结果。
public static string getDocumentAsXaml(IDocumentPaginatorSource flowDocument)
{
return XamlWriter.Save(flowDocument);
}
回答by user1359009
Using conn As New System.Data.SqlClient.SqlConnection(connectionSTRING)
Dim adapter As New System.Data.SqlClient.SqlDataAdapter(selectSTRING, conn)
Dim DS As System.Data.DataSet = New System.Data.DataSet
adapter.Fill(DS)
Dim ba() As Byte = Text.Encoding.ASCII.GetBytes(DS.Tables(0).Rows(0)("RTF_Field").ToString())
Dim ms As MemoryStream = New MemoryStream(ba)
Dim fd As FlowDocument = New FlowDocument
Dim tr As TextRange = New TextRange(fd.ContentStart, fd.ContentEnd)
tr.Load(ms, System.Windows.DataFormats.Rtf)
ms.Close()
RichTextBox.Document = fd
End Using
You will need to use your connection string & SQL select statement ... other than that, this is it ...
您将需要使用您的连接字符串和 SQL 选择语句......除此之外,就是这样......