Set RTF text into WPF RichTextBox control

前端 未结 5 1186
一个人的身影
一个人的身影 2021-01-31 08:44

I have this RTF text:

{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}
{\\colortbl ;\\red0\\green0\\blue0;\\red255\\green0\         


        
5条回答
  •  野的像风
    2021-01-31 09:24

    Do you really have to start with a string?

    One method to load RTF is this:

    rtfBox.Selection.Load(myStream, DataFormats.Rtf);
    

    You probably should call SelectAll() before that if you want to replace existing text.

    So, worst case, you'll have to write your string to a MemoryStream and then feed that stream to the Load() method. Don't forget to Position=0 in between.

    But I'm waiting to see somebody to come up with something more elegant.

提交回复
热议问题