Uzair Ali
Uzair Ali

Reputation: 530

How to append RTF text in richtextbox in WPF with C#

I have string of RTF and want to append that RTF in other RichTextBox I am using richtextbox1.AppendText(RTFString); but that appending all the text without styling the text

string RTFString = {\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}        {\f2\fcharset0 Segoe UI;}{\f3\fcharset0 Blackadder ITC;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs21\f2\cf0 \cf0\ql\sl20\slmult0{\fs108\f3 {\ltrch uzair}\li0\ri0\sa0\sb0\fi0\ql\sl20\slmult0\par}
}

}{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard \{\\rtf1\\ansi\\ansicpg1252\\uc1\\htmautsp\\deff2\{\\fonttbl\{\\f0\\fcharset0 Times New Roman;\}\{\\f2\\fcharset0 Segoe UI;\}\{\\f3\\fcharset0 Blackadder ITC;\}\}\{\\colortbl\\red0\\green0\\blue0;\\red255\\green255\\blue255;\}\\loch\\hich\\dbch\\pard\\plain\\ltrpar\\itap0\{\\lang1033\\fs21\\f2\\cf0 \\cf0\\ql\\sl20\\slmult0\{\\fs108\\f3 \{\\ltrch uzair\}\\li0\\ri0\\sa0\\sb0\\fi0\\ql\\sl20\\slmult0\\par\} \par \} \par \} }

Appending

richtextBox1.AppendText(RTFString);

Output

Same as the "RTFString" text

Optional While styling fontFamily, if the fontFamily have fallback setting it will be great

Upvotes: 0

Views: 2952

Answers (1)

slava
slava

Reputation: 1915

FlowDocument document = new FlowDocument();
Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Bold(new Run("Your text")));
document.Blocks.Add(paragraph);
richTextBox1.Document = document;

Use Paragraph class and add it as a document. Hope it will help.

EDIT

If you talk about rtf text then try this

MemoryStream stream = new MemoryStream(ASCIIEncoding.Default.GetBytes(text));
this.mainRTB.Selection.Load(stream, DataFormats.Rtf);

Upvotes: 4

Related Questions