rtf到文本块

时间:2012-04-20 20:46:04

标签: c# wpf xaml document rtf

当我尝试使用rtf设置文本块时,它会给出一个有趣的输出,是否有办法在文本块中显示rtf,如果是这样的话?

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?

4 个答案:

答案 0 :(得分:2)

您无法使用TextBlock显示RTF文本。但是如果可以在FlowDocumentScrollViewer中显示文本,则可以这样复制:

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

概述Flow Documents here

答案 1 :(得分:1)

这将为您提供整个FlowDocument,但好消息是包含标记。我认为这就是你要找的东西

string textMarkUp = System.Windows.Markup.XamlWriter.Save(richTextBox1.Document);
Debug.WriteLine(textMarkUp); 

示例输出

<Paragraph>asdfas<Run FontWeight="Bold">adsfasd;lkasdf</Run><Run FontStyle="Italic" FontWeight="Bold">alskjfd</Run></Paragraph>

答案 2 :(得分:0)

我需要Text Block,因为它可以扩展为内容,我们可以将wrap设置为none。我在数据库中存储rtf字符串。我将字符串添加到RichTextBlock,然后使用其文档来获取内联。

    Dim stream As New IO.MemoryStream(System.Text.ASCIIEncoding.[Default].GetBytes("{\rtf1\ansi\ansicpg1252\deff0\deflang1040{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}{\colortbl ;\red255\green255\blue255;}\viewkind4\uc1\pard\cf1\f0\fs29 RIGO NOTIZIA 1 TESTO TESTO TESTO\fs17\par}"))
    Dim RichTextBox1 As New RichTextBox()
    RichTextBox1.Selection.Load(stream, DataFormats.Rtf)

    Dim pr As New System.Windows.Documents.Paragraph()
    pr = RichTextBox1.Document.Blocks(0)
    Dim tre As Int32 = pr.Inlines.Count
    TextBlock1.Inlines.Add(pr.Inlines(0))

答案 3 :(得分:0)

RichTextBox仅用于转换,最终控件是FlowDocumentScrollViewer,因此我的功能略有简化:

public static class FlowDocumentScrollViewerEx
{
    static public bool ReadFromFile(this FlowDocumentScrollViewer fDoc, String rtfFilePath)
    {
        RichTextBox retext = new RichTextBox();     // Just an intermediate class to perform conversion
        retext.Document = new FlowDocument();
        fDoc.Document = new FlowDocument();

        TextRange tr = new TextRange(retext.Document.ContentStart, retext.Document.ContentEnd);

        if (!File.Exists(rtfFilePath))
            return false;

        using (var fs = new FileStream(rtfFilePath, FileMode.OpenOrCreate))
        {
            tr.Load(fs, DataFormats.Rtf);
            fs.Close();
        }

        MemoryStream ms = new MemoryStream();
        System.Windows.Markup.XamlWriter.Save(retext, ms);
        tr.Save(ms, DataFormats.XamlPackage);
        TextRange flowDocRange = new TextRange(fDoc.Document.ContentStart, fDoc.Document.ContentEnd);
        flowDocRange.Load(ms, DataFormats.XamlPackage);
        return true;
    } //ReadFromFile
} //class FlowDocumentScrollViewerEx

用法非常简单:

flowDocument.ReadFromFile(@"license.rtf");