0
votes

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. RichTextBox Example

After saving the file it looks like this when i open the RTF in word. Bad Output

This is what I want it to output. enter image description here

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.

3
How do you append text in your richtext control?Nicolas C
Could you put the string stored in textrange before conversion to rtf? I think the \r\n are not conservedNicolas C

3 Answers

0
votes

It will be more simple to answer if you put some example of rtf code you try to save, the result you have and what you want.

I'm not sure of what you want to be in multiple line. The rtf in a more easiest "reading" format for human? Where one line in richtextbox is on one line in rtf format like this:

{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1036\fs18\f2\cf0 \cf0\ql
{\f2 {\ltrch aaaaa}\li0\ri0\sa0\sb0\fi0\ql\par}
{\f2 {\ltrch bbbb}\li0\ri0\sa0\sb0\fi0\ql\par}
{\f2 {\ltrch }\li0\ri0\sa0\sb0\fi0\ql\par}
{\f2 {\ltrch cccc}\li0\ri0\sa0\sb0\fi0\ql\par}
}
}

in richtextbox I see this

aaaa
bbbb

cccc

I'm not sure of the interest to do that, because newline is encode by \par for end of paragraphs and \line for new line in the current paragraph then when you open the rtf file with a program (MS Word for example) newlines are respected.

And I obtain the same with this:

{\rtf1\ansi\ansicpg1252\uc1\htmautsp\deff2{\fonttbl{\f0\fcharset0 Times New Roman;}{\f2\fcharset0 Segoe UI;}}{\colortbl\red0\green0\blue0;\red255\green255\blue255;}\loch\hich\dbch\pard\plain\ltrpar\itap0{\lang1036\fs18\f2\cf0 \cf0\ql {\f2 {\ltrch aaaaa}\li0\ri0\sa0\sb0\fi0\ql\par}{\f2 {\ltrch bbbb}\li0\ri0\sa0\sb0\fi0\ql\par}{\f2 {\ltrch }\li0\ri0\sa0\sb0\fi0\ql\par}{\f2 {\ltrch cccc}\li0\ri0\sa0\sb0\fi0\ql\par}}}
0
votes

Don't add the rtf code directly, use Wpf Paragraph and so on.

When you add your new line to RichTextBox use

YourParagraph.Inlines.Add(new LineBreak());

instead of \r\n or something like this.

0
votes

As you can see in this post, if you add text as plaintext programaticaly to the RichTextBox control, you may have to add this unicode line separator \u2028. And it will normaly converted to \line in the Rtf saved file.

And I recommend you to use the method with TextRange.