How to convert FlowDocument to rtf
Asked Answered
L

3

10

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

Learning answered 6/5, 2009 at 10:49 Comment(2)
Perhaps you could provide a code sample? What differs between you code and Peter's answer (which seems workable according to MSDN)?Lease
I found this link looks very helpful matthewmanela.com/blog/converting-rtf-to-htmlDenesedengue
A
29

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))
{
    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);
}
Alexio answered 6/5, 2009 at 11:4 Comment(1)
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 flowDodcumentLearning
A
2

This works like a charm for me. Displays the result in an RTF box without difficulties.

public static string getDocumentAsXaml(IDocumentPaginatorSource flowDocument)
{
     return XamlWriter.Save(flowDocument);
}
Alkylation answered 22/5, 2009 at 11:26 Comment(0)
P
-4
    Using conn As New System.Data.SqlClient.SqlConnection(connectionSTRING)
       Dim adapter As New System.Data.SqlClient.SqlDataAdapter(selectSTRING, conn)
       Dim DS As System.Data.DataSet = New System.Data.DataSet
       adapter.Fill(DS)

       Dim ba() As Byte = Text.Encoding.ASCII.GetBytes(DS.Tables(0).Rows(0)("RTF_Field").ToString())

       Dim ms As MemoryStream = New MemoryStream(ba)
       Dim fd As FlowDocument = New FlowDocument
       Dim tr As TextRange = New TextRange(fd.ContentStart, fd.ContentEnd)
       tr.Load(ms, System.Windows.DataFormats.Rtf)
       ms.Close()

            RichTextBox.Document = fd

        End Using

You will need to use your connection string & SQL select statement ... other than that, this is it ...

Prefab answered 26/4, 2012 at 14:55 Comment(3)
He doesn't address the problem. The code just reads an RTF string from a binary database field (as ASCII >.<), and then loads it into a flow document as RTF.Fasta
Accepted Answer: use the TextRange class to Save and Load to various formats including Rtf.Prefab
The solution offered here uses textrange to load memorystream, DataFormats.RTF ... same as accepted ... just displayed a field grab ... you guys troll much >.<Prefab

© 2022 - 2024 — McMap. All rights reserved.