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

4

11

Could anyone please help me to create RTF from string in C#?

I save all the formats (bold, italic, etc) in a customized class.

Jerry

Log answered 10/9, 2012 at 8:9 Comment(0)
B
5

I'd use an external library instead of coding your own. Check these projects:

You might also be able to use the Rtf property of the RichTextBox control (if your users are entering data in such a control).

Backward answered 10/9, 2012 at 8:12 Comment(3)
netrtfwriter is just amazing, download and run in VS2017 without any trouble, perfect result. It even show a table with complex cells. Detailed documentation in Program.cs.Mezereum
You may consider focusing on netrtfwriter in your main answer text since the question is about C#, then mention the C code in a note at the end. I thought your answer is about C and go to check other answersMezereum
@Mezereum the second link is about C# as well, it's just the URL that dropped the '#'.Backward
B
7

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

richTextBox1.SaveFile(@"C:\temp\test.rtf", RichTextBoxStreamType.RichText);
Backset answered 10/9, 2012 at 8:12 Comment(0)
B
5

I'd use an external library instead of coding your own. Check these projects:

You might also be able to use the Rtf property of the RichTextBox control (if your users are entering data in such a control).

Backward answered 10/9, 2012 at 8:12 Comment(3)
netrtfwriter is just amazing, download and run in VS2017 without any trouble, perfect result. It even show a table with complex cells. Detailed documentation in Program.cs.Mezereum
You may consider focusing on netrtfwriter in your main answer text since the question is about C#, then mention the C code in a note at the end. I thought your answer is about C and go to check other answersMezereum
@Mezereum the second link is about C# as well, it's just the URL that dropped the '#'.Backward
I
4

I know i'm NECRO an old question but, anyways, 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 a file_path and the 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 it helps.

Ilocano answered 6/4, 2017 at 18:27 Comment(0)
C
3

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

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

its free, and just needs a bit of documentation, also you can use the NuGet package manager to find lots of alternatives.

Cruiserweight answered 10/9, 2012 at 8:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.