How to create RTF from plain text (or string) in C #?

Can someone help me create an RTF from a string in C #?

I save all formats (in bold, italics, etc.) in a custom class.

Jerry

+7
source share
4 answers

I would use an external library instead of encoding it. Check out these projects:

You can also use the Rtf property of the RichTextBox control (if your users enter data into such a control).

+4
source

As the simplest option, you can use RichTextBoxControl in a winforms application.

richTextBox1.SaveFile(@"C:\temp\test.rtf", RichTextBoxStreamType.RichText); 
+4
source

A good library for working / Creating / Editing RTF files can be found here:

http://sourceforge.net/projects/netrtfwriter/

its free and it just needs some documentation, you can also use the NuGet package manager to find many alternatives.

+1
source

I know that I'm NECRO - an old question, but anyway, here is my apport:

 //This Instances a new RichTextBox Control and uses it so save the Text private void Save_RTF_file(string pFilePath, string pRTFText) { try { using (RichTextBox RTB = new RichTextBox()) { RTB.Rtf = pRTFText; RTB.SaveFile(pFilePath, RichTextBoxStreamType.RichText); } } catch (Exception ex) { throw ex; } } 

Now you pass the path file_path and RTF formatted text:

 //This is a simple 1 line 'Hello World' RTF text string RTF = @"{\rtf1\ansi\ansicpg1252\deff0\nouicompat\deflang14346{\fonttbl{\f0\fnil\fcharset0 Calibri;}} {\*\generator Riched20 10.0.10586}\viewkind4\uc1 \pard\sa200\sl276\slmult1\f0\fs22\lang10 Hello World.\par }"; Save_RTF_file(@"C:\temp\my_rtf_file.rtf"), RTF); 

Hope this helps.

+1
source

All Articles