如何将内容从richtextbox保存到RTF文件中? [英] How can I save content from richtextbox into a RTF file?

查看:252
本文介绍了如何将内容从richtextbox保存到RTF文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FlowDocument document = rtt.Document;
System.IO.Stream ms = new System.IO.MemoryStream();
System.Windows.Markup.XamlWriter.Save(document, ms);
byte[] data = new byte[ms.Length];
ms.Position = 0;
ms.Read(data, 0, data.Length);
ms.Close();
File.WriteAllBytes(THE PATH, data);

通过上面的代码,我将富文本框(命名为rtt)的内容放入流中,然后将该流保存到.rtf文件中.
由于我通过BlockUIContainer将一个小的richtextbox嵌套到了一个大的richtexbox(名为rtt)中,所以我想我不能使用  richTextBox.Document.ContentStart,
  richTextBox.Document.ContentEnd,(我想这仅适用于纯文本吗?)我试图将Richtextbox中的内容转换为二进制流.

但是问题在于,在保存的文件中,richtextbox中的内容以以下格式显示:

< FlowDocument" PagePadding =" 5,0,5,0"  AllowDrop =" True"  xmlns =" http://schemas.microsoft.com/winfx/2006/xaml/presentation> < Paragraph> RichTextBoxggg< Run  xml:lang ="en"  xml:space =" preserve">它 只是  a 测试!!!!</Run> /Paragraph>< BlockUIContainer>< RichTextBox  IsReadOnly ="True""名称="rtb2">< FlowDocument  PagePadding =``5,0,5,0''" nbsp; Allow ;>" Paragraph> sdlf</Paragraph></FlowDocument></RichTextBox></BlockUIContainer></FlowDocument>

By the code above, I put the content of a richtextbox(named as rtt)into a stream, then save this stream into a .rtf file. 

Since I nested a small richtextbox into the big richtexbox(named as rtt) by BlockUIContainer, so I think I can not use  richTextBox.Document.ContentStart,
 richTextBox.Document.ContentEnd, (i suppose this is only suitable for pure text?) I tried to convert the content in richtextbox into binary stream.

But the problem is, in the saved file, the content from the richtextbox is shown in below format:

<FlowDocument PagePadding="5,0,5,0" AllowDrop="True" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph>RichTextBoxggg<Run xml:lang="en" xml:space="preserve"> it is just a test!!!!</Run></Paragraph><BlockUIContainer><RichTextBox IsReadOnly="True" Name="rtb2"><FlowDocument PagePadding="5,0,5,0" AllowDrop="True"><Paragraph>sdlf</Paragraph></FlowDocument></RichTextBox></BlockUIContainer></FlowDocument>

它以xaml格式显示,但是我希望它以WPF格式显示(在WPF中设计视图时,),

It is shown as xaml format, but i want it shown as the format in WPF (the view when i design  in WPF, ), 

那么我该如何将richtexbox的内容转换为二进制流,然后将其另存为rtf文件到磁盘中(并显示为wpf视图,而不是xaml格式)

So How can I convert the content of a richtexbox to binary stream, and then save it as a rtf file in a disk (and show as the wpf view, not the xaml format)

推荐答案

尝试以下链接:

Try the following link:

http://umaranis.wordpress.com/2010/11/29/save-and-load-richtextbox-content-in-wpf/

http://umaranis.wordpress.com/2010/11/29/save-and-load-richtextbox-content-in-wpf/


这篇关于如何将内容从richtextbox保存到RTF文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆