For a project I am working on I am trying to save the contents of a WPF RichTextBox to a RTF file as the title states. I have it working for the most part. However, the file does not preserve newlines. When I save the file ( as you will see below ) it will save everything to one line in the RTF. You can see how it is saved below.
private void butSaveHistory_Click( object sender, RoutedEventArgs e ) {
Microsoft.Win32.SaveFileDialog myDlg = new Microsoft.Win32.SaveFileDialog();
myDlg.DefaultExt = "*.rtf";
myDlg.Filter = "RTF Files|*.rtf";
Nullable<bool> myResult = myDlg.ShowDialog();
if ( myResult == true ) {
/*using ( FileStream myStream = new FileStream( myDlg.FileName, FileMode.OpenOrCreate, FileAccess.Write ) ) {
TextRange myRange = new TextRange( rtbTraffic.Document.ContentStart, rtbTraffic.Document.ContentEnd );
myRange.Save( myStream, DataFormats.Rtf );
myStream.Close();
}*/
rtbTraffic.SelectAll();
rtbTraffic.Selection.Save( new FileStream( myDlg.FileName, FileMode.OpenOrCreate, FileAccess.Write ), DataFormats.Rtf );
}
}
AS you can see I tried two different ways. ( One is commented out ) Neither of these work, they both just save everything to one line when everything in the RichTextBox is on multiple lines.
So how can I get the file to save to multiple lines? Any tips or suggestions would be greatly appreciated.
Note: When saving to a .txt file it saves correctly to multiple lines. However, I cannot save to .txt because the colors and fonts of the RichTextBox need to be preserved.
Edit: This is a sample of what the RichTextBox looks like.
After saving the file it looks like this when i open the RTF in word.
This is what I want it to output.
Edit 2: Adding RTF code
{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}{\f3\fcharset0 Arial;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red0\green128\blue0;\red0\green0\blue255;\red255\green0\blue0;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1033\fs18\f2\b\cf0 \cf0\ql{\f3 {\b0\cf2\highlight1\ltrch 09:10:48 | Thing | STATUS Tube_Heat_Consumer,TUBE_HEAT,3.14209:11:47 | Thing | STATUS Tube_Heat_Consumer,TUBE_HEAT,2.718}{\b0\cf3\highlight1\ltrch 09:58:49 | Thing | STOP STOP}{\b0\cf2\highlight1\ltrch 09:58:49 | Thing | STOP STOP}{\b0\highlight1\ltrch 09:58:57 | Thing | DeRegistration Successful}{\b0\cf4\highlight1\ltrch 09:58:58 | Thing | DeRegistration Failed ( Application is not currently registered ) | 81270401}{\b0\highlight1\ltrch 09:58:58 | Thing | Registration Successful}\li0\ri0\sa0\sb0\fi0\ql\par}
}
} Thing | DeRegistration Failed ( Application is not currently registered ) | 81270401}{\b0\highlight1\ltrch 08:55:21 | Thing | Registration Successful08:55:22 | Thing | DeRegistration Successful08:55:22 | Thing | Registration Successful}{\b0\cf2\highlight1\ltrch 08:55:22 | Thing | Registration Failed ( Application Thing already registered ) | 8127040008:55:22 | Thing | Registration Failed ( Application Thing already registered ) | 81270400}{\b0\highlight1\ltrch 08:55:23 | Thing | DeRegistration Successful08:55:23 | Thing | Registration Successful08:55:24 | Thing | DeRegistration Successful08:55:24 | Thing | Registration Successful08:55:25 | Thing | DeRegistration Successful}\li0\ri0\sa0\sb0\fi0\ql\par}
}
}
From the looks of it it is not encoding \par or \lines correctly.