1

当我尝试使用 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

4 回答 4

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

在此处获取流程文档的概述。

于 2012-04-20T21:49:56.650 回答
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>
于 2012-04-20T21:47:29.637 回答
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))
于 2013-06-14T10:12:18.440 回答
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");
于 2016-05-09T08:45:16.027 回答