1

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;

Edit update:

I can do this:

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

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?

Kirsty White
  • 1,210
  • 3
  • 26
  • 54
  • 2
    I may be wrong, but this looks an awful lot like a doublepost of http://stackoverflow.com/questions/10252506/richtextbox-to-string – Jasper Apr 20 '12 at 20:52
  • 1
    how different is this new question? – nawfal Apr 20 '12 at 21:02
  • Are you just trying to display the raw RTF in a textblock or are you hoping the TextBlock will display the text applying the RTF? – Josh Apr 20 '12 at 21:05
  • Hey josh I was hoping it will be displayed with applied rtf, I have tryed with a document viewer just dont know how to :( – Kirsty White Apr 20 '12 at 21:10
  • Shame you're not using Silverlight - you could use the [`Xaml`](http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.xaml(v=vs.95).aspx) property – ChrisF Apr 20 '12 at 21:25
  • If you had not deleted my comment I have the answer. – paparazzo Apr 20 '12 at 21:33
  • I cant delete your comments Blam, I did see some one had answered then when I went to check there was nothing there...? I also cant delete answers. – Kirsty White Apr 20 '12 at 21:34
  • @ChrisF I knew I should have went with silverlight... – Kirsty White Apr 20 '12 at 21:36
  • @Blam: Who deleted your comment? What was the comment? – BoltClock Apr 20 '12 at 21:50
  • Blam are you drunk lol you said that on this question http://stackoverflow.com/questions/10252506/richtextbox-to-string not on this one... you should use the "Favourite" question marker to get back and forth to a users questions rather than using their profile (if thats how you got to this question) You got me worryed thinking someone had a vendetta against me or you. – Kirsty White Apr 20 '12 at 21:58
  • 1
    Way sorry. Maybe I have right answer to that question. – paparazzo Apr 20 '12 at 22:00

4 Answers4

2

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

Get an overview of Flow Documents here.

Clemens
  • 123,504
  • 12
  • 155
  • 268
1

This is going to give you the whole FlowDocument but the good news is that does include the markup. I assume that is what you are looking for

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

Sample output

<Paragraph>asdfas<Run FontWeight="Bold">adsfasd;lkasdf</Run><Run FontStyle="Italic" FontWeight="Bold">alskjfd</Run></Paragraph>
paparazzo
  • 44,497
  • 23
  • 105
  • 176
0

I needed Text Block because it can expand to contents and we can set wrap as none. I am storing rtf string in database. I added the string to RichTextBlock and then used its document to get the inline.

    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))
Leon Munir
  • 51
  • 9
0

RichTextBox is used just for conversion, final control is FlowDocumentScrollViewer, so I've ended up with slightly simplified function:

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

Usage is quite trivial:

flowDocument.ReadFromFile(@"license.rtf");
TarmoPikaro
  • 4,723
  • 2
  • 50
  • 62