How to get RTF from RichTextBox

前端 未结 2 1562
离开以前
离开以前 2020-12-09 05:24

How do I get the text in RTF of a RichTextBox? I\'m trying to get like this, but the property does not exist.

RichTextBox rtb = new RichTextBox(         


        
2条回答
  •  情书的邮戳
    2020-12-09 06:17

    To get the actual XAML created by the user inside of the RichTextBox:

       TextRange tr = new TextRange(myRichTextBox.Document.ContentStart,
                                    myRichTextBox.Document.ContentEnd);
       MemoryStream ms = new MemoryStream();
       tr.Save(ms, DataFormats.Xaml);
       string xamlText = ASCIIEncoding.Default.GetString(ms.ToArray());
    

    EDIT: I don't have code in front of me to test, but an instance of the TextRange type has a Save (to stream) method that takes a DataFormats parameter, which can be DataFormats.Rtf

提交回复
热议问题