views:

2075

answers:

2

Hi,

I have used a WPF RichTextBox to save a flowdocument from it as byte[] in database. Now i need to retrieve this data and display in a report RichTextBox as an rtf. when i try to convert the byte[] using TextRange or in XAMLReader i get a FlowDocument back but how do i convert it to rtf string as the report RichTextBox only takes rtf.

Thanks

Arvind

+2  A: 

You should not persist the FlowDocument directly as it should be considered the runtime representation of the document, not the actual document content. Instead, use the TextRange class to Save and Load to various formats including Rtf.

A quick sample on how to create a selection and save to a stream:

var content = new TextRange(doc.ContentStart, doc.ContentEnd);

if (content.CanSave([DataFormats.Rtf][2]))
{
    using (var stream = new MemoryStream())
    {
        content.Save(stream, DataFormats.Rtf);
    }
}

To load content into a selection would be similar:

var content = new TextRange(doc.ContentStart, doc.ContentEnd);

if (content.CanLoad(DataFormats.Rtf))
{
    content.Load(stream, DataFormats.Rtf);
}
Peter Lillevold
Thanks for your responce but i have tryed this method and this this does not save the formating made in the text(ie making text bold changing colour),i need to also the save the format so i save it as a flowDodcument
A: 

This works like a charm for me:

public static  string getDocumentAsXaml(IDocumentPaginatorSource flowDocument)
{
     return XamlWriter.Save(flowDocument);
}

the result should be displayed in a rtf box without difficulties.

Nasit