2012-04-20 60 views
1

当我尝试设置一个文本块与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?

+2

我可能是错误的,但这看起来很像一个http://stackoverflow.com/questions/10252506/richtextbox-to-string – Jasper 2012-04-20 20:52:28

+1

二重柱这个新问题有什么不同? – nawfal 2012-04-20 21:02:16

+0

你只是试图在文本块中显示原始RTF,或者你是否希望TextBlock将显示应用RTF的文本? – Josh 2012-04-20 21:05:42

回答

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

获取流文件here的概述。

+0

+1谢谢克莱门斯,希望有更好的范围的rtf控件vs vs悲伤 – 2012-04-20 21:52:40

+0

RichTextBox和3种类型的FlowDocument查看器。你在找什么? – Paparazzi 2012-04-21 01:00:16

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> 
+0

不,我不认为这是我想要的......? – 2012-04-20 21:49:13

+0

那你想要什么?请发布样品。 – Paparazzi 2012-04-20 21:50:55

0

我需要文本块,因为它可以扩展到内容,我们可以将包装设置为无。我在数据库中存储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)) 
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");