Question

I found how to make text bold in code:

richTextBox1.Rtf = @"{\rtf1\ansi This is in \b bold\b0.}";

But I also need how to make text italic. Google doesn't give me much.

I tried this (similar to bold, but with different character) but that doesn't work.

richTextBox1.Rtf = @"{\rtf1\ansi This is in \i italic\i0.}";

Can someone help me out please?

Was it helpful?

Solution 2

this is how I managed to do it:

richTextBox1.Rtf = @"{\rtf1\ansi This is in \i\f0\fs17 italic\i0.}";

edit:

How did I do this? I created a small test-application with a richtextbox and a button.

I typed some text in the richtextbox, I selected the text and pressed the button.

private void button1_Click(object sender, EventArgs e)
{
    richTextBox1.SelectionFont = new Font(richTextBox1.Font, FontStyle.Italic);
    richTextBox1.SaveFile(@"c:\test.rtf");
}

This saved the rtf. I opened the rtf in Notepad++. The content of the rtf was

{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
\viewkind4\uc1\pard\i\f0\fs17 hello\i00\par
}

and that's how I found how to use italics in a richtextbox.

OTHER TIPS

There are two articles that comes to my mind that may help you understand RTF, the first is a RTFTree which can be used to build a complex document and loads it akin to an XML document loading where you have trees/nodes. The other article is about writing your own RTF converter - a parser that can convert a RTF to HTML and vice versa.

You will find samples in the code on how to insert a italic formatting and so on. I included these two links to help give you insight into how to use RTF.

Begin Edit: I have created a simple rtf document in WordPad as shown here

{\rtf1\ansi\ansicpg1252\deff0\deflang6153{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
{\*\generator Msftedit 5.41.15.1515;}\viewkind4\uc1\pard\b\f0\fs20 Bold\b0\par
\i Italic\i0\par
}

The RTF document has two lines 'Bold' and 'Italic' with their respective formatting, saved the document and opened it up in another editor, that is what is shown. So something must be missing perhaps a paragraph marker \par wrapped around it.

This was done under Windows XP Home's WordPad.

End Edit

Hope these will be of help and use to you, Best regards, Tom.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top