rtf 到文本块 [英] rtf to textblock

查看:36
本文介绍了rtf 到文本块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 rtf 设置文本块时,它给出了一个有趣的输出,如果可以,有没有办法在文本块中显示 rtf?

When I try to set a textblock with rtf it gives a funny output is there a way to display rtf in a textblock if so how?

private void button1_Click(object sender, RoutedEventArgs e)
{
    TextRange tr = new TextRange(richTextBox1.Document.ContentStart,
                     richTextBox1.Document.ContentEnd);
    MemoryStream ms = new MemoryStream();
    tr.Save(ms, DataFormats.Rtf); 
    string rtfText = ASCIIEncoding.Default.GetString(ms.ToArray());
    textBlock1.Text = rtfText;

编辑更新:

我可以这样做:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        TextRange tr = new TextRange(richTextBox1.Document.ContentStart,
             richTextBox1.Document.ContentEnd);
        MemoryStream ms = new MemoryStream();
        tr.Save(ms, DataFormats.Rtf); // does not contain a definition
        string rtfText = ASCIIEncoding.Default.GetString(ms.ToArray());
        MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(rtfText));
           this.richTextBox2.Selection.Load(stream, DataFormats.Rtf);

但是我真的很讨厌richtextbox 难道没有其他控件可以保存富文本格式吗?或者有什么方法可以告诉某个控件显示rtf?

But I really hate the richtextbox is there no other controls that can hold rich text formatting? Or is there a way in which you can tell a certain control to display rtf?

推荐答案

您不能使用 TextBlock 来显示 RTF 文本.但是,如果可以在 FlowDocumentScrollViewer 中显示文本,您可以复制这样:

You can't use a TextBlock to display RTF text. But if it's ok to show the text in a FlowDocumentScrollViewer, you could copy it this way:

public MainWindow()
{
    InitializeComponent();

    richTextBox.Document = new FlowDocument();
    flowDocumentScrollViewer.Document = new FlowDocument();
}

private void CopyDocument(FlowDocument source, FlowDocument target)
{
    TextRange sourceRange = new TextRange(source.ContentStart, source.ContentEnd);
    MemoryStream stream = new MemoryStream();
    XamlWriter.Save(sourceRange, stream);
    sourceRange.Save(stream, DataFormats.XamlPackage);
    TextRange targetRange = new TextRange(target.ContentStart, target.ContentEnd);
    targetRange.Load(stream, DataFormats.XamlPackage);
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    CopyDocument(richTextBox.Document, flowDocumentScrollViewer.Document);
}

此处获取流程文档概览.

这篇关于rtf 到文本块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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