How to convert FlowDocument to rtf

前端 未结 3 1976
-上瘾入骨i
-上瘾入骨i 2020-12-15 10:46

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 tr

3条回答
  •  生来不讨喜
    2020-12-15 10:53

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

提交回复
热议问题