Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load a .rtf File into a RichTextBox and Maintain/Persist the Formatting

All, I write a log file to a .rtf file which has formatting underlining, bold etc. I have saved this file and want to read it back into the RichTextBox at a later time persisting its formatting. I have tried the following

tmpRichTextBox.LoadFile(@"F:\Path\File.rtf", RichTextBoxStreamType.RichText);

It loads the file but there is none of my original formatting. If I load the .rtf into word, the formatting shows up. How do I read the .rtf back into the RichTextBox including its formatting?

Thanks for your time.

like image 682
MoonKnight Avatar asked May 18 '12 17:05

MoonKnight


3 Answers

Did you Check NRTFTree.

Its an awesome library for RTF management!

like image 163
Writwick Avatar answered Oct 31 '22 15:10

Writwick


Edited:
It's possible that you are losing the formating later in code. There are certain operations that may cause the loss of the formating. For example, richTextBox.Font = newFont;

I had this problem, but luckily I found a way around it. Here's the code that will let you change the font without losing the formating:

richTextBox.SelectAll();
richTextBox.SelectionFont = newFont;
string rtf = richTextBox.SelectedRtf;
richTextBox.Font = newFont;
richTextBox.Rtf = rtf;
like image 40
Dzienny Avatar answered Oct 31 '22 14:10

Dzienny


If you can save your log file to html format you can read this file with WebBrowser Control. Like this:

  private void button1_Click(object sender, EventArgs e)
  {
     string fileName = Path.Combine(Environment.CurrentDirectory, "log1.htm");

      webBrowser1.DocumentText = File.ReadAllText(fileName);
   }

This works perfectrly.

like image 1
Vladimir Kniazkov Avatar answered Oct 31 '22 14:10

Vladimir Kniazkov