tags:

views:

62

answers:

2

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?

+1  A: 

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.

tommieb75
thanks for your input, bit it's not clear what to use for italic text
Natrium
@Natrium: Please see my edited answer. My guess is you have not selected the appropriate font to enable the bold/italic styling. Perhaps try copy the rtf code and paste it as an assignment to the property RtfText of the richTextBox1 class. Hope this is of further help.
tommieb75
+1 for your effort
Natrium
A: 

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.

Natrium