Cómo escribir varios contenidos de RichTextBox en un archivo RTF conservando los formatos de fuente de cada RichTextBox

I have a Winforms project where I can write text into a RichTextBox, and some controls to set the font formats of the written text. I am able to save and append text to RTF file, but I'm having a problem of preserving formatos de fuente of each RichTextBox. Any Help?

CÓDIGO:

RichTextBox r1 = new RichTextBox();
RichTextBox r2 = new RichTextBox();
string nickName = "Test: ";
string message = "Hi this is a test message";
r1.Text = nickName;
r1.ForeColor = Color.Blue;

r2.Text = message;
r2.ForeColor = Color.Black;

string path = @"d:\Test.rtf";
if (!File.Exists(path))
{
    using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
    using (StreamWriter sw = new StreamWriter(fs))
    {
        sw.WriteLine(r1.Rtf);
        sw.WriteLine(r2.Rtf);
        sw.Close();
    }
}
else
{
    using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
    using (StreamWriter sw = new StreamWriter(fs))
    {
        sw.WriteLine(r1.Rtf);
        sw.WriteLine(r2.Rtf);
        sw.Close();
    }
}

preguntado el 13 de septiembre de 13 a las 11:09

Do you mean when you open your Test.rtf file (such as with WordPad) it doesn't show the correct ForeColor? Any more details? -

yes, when I open the test.rtf, in this case, either all the text is printed in 1 format, or only the content of the first RichTextBox is saved. -

1 Respuestas

You can avoid this problem by merging all the content into the same RichTextBox. Muestra:

r1.Text = r1.Text + Environment.NewLine;
r1.SelectAll();
r1.Copy();
r2.Paste();

r2.SaveFile(path);

This approach works fine with StreamWriter as you were using it. On the other hand, why not using a simpler/specifically-designed-for-this-purpose method (SaveFile)? If you don't want to replace the contents in r2, you can just rely on a temporary RichTextBox:

 r1.Text = r1.Text + Environment.NewLine;
 r1.SelectAll();
 r1.Copy();
 RichTextBox temp = new RichTextBox();
 temp.Paste();
 r2.SelectAll();
 r2.Copy();
 temp.Paste();

 temp.SaveFile(path);

NOTE: there might be problems when using StreamWriter (to append, for example). Bear in mind that RTF is a special format which requires a special treatment: perform any modification from the RichTextBox control (add, remove, edit, etc. text/format) and rely on the methods LoadFile y SaveFile, rather than in the ones for TXT files (i.e., StreamReader/StreamWriter).

Respondido el 13 de Septiembre de 13 a las 15:09

Everything works, but I am not able to append to the same file extra text... how can I do that? - Samer_Azar

I mean my method above while using streamWriter to append: works fine with plain text, but does not work with RichTextBox: using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write)) using (StreamWriter sw = new StreamWriter(fs)) { sw.WriteLine(r2.Rtf); sw.Close(); } ¿ALGUNA SUGERENCIA? - Samer_Azar

@samerAzar you are right, there are problems while appending it. Bear in mind that rtf is an "special format" (like MS Office Word). I tested the StreamWriter and it worked and that's why I assumed that you can use it without any restriction, but apparently this is not the case. The best way to avoid problems is rely on the specific methods, in this case: SaveFile and LoadFile (ignore streamWriter). If you want to override the 1.rtf, use richTextBox.LoadFile("1.rtf") add anything you want to richTextBox and save it with richTextBox.SaveFile("1.rtf"). - varocarba

@samerAzar I have added a clarification to my answer saying basically the same than in my previous comment: better don't treat RTFs as TXTs (= rely on StreamWriter/StreamReader as less as possible). - varocarba

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.