Kirsty White
Kirsty White

Reputation: 1220

rtf to textblock

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?

Upvotes: 1

Views: 4695

Answers (4)

TarmoPikaro
TarmoPikaro

Reputation: 5253

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

Upvotes: 0

Leon Munir
Leon Munir

Reputation: 51

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

Upvotes: 0

Clemens
Clemens

Reputation: 128136

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.

Upvotes: 3

paparazzo
paparazzo

Reputation: 45106

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>

Upvotes: 1

Related Questions