首页 文章

将RichTextBox保存到RTF文件

提问于
浏览
0

对于我正在处理的项目,我正在尝试将WPF RichTextBox的内容保存为RTF文件,如 Headers 所示 . 我大部分都在工作 . 但是,该文件不保留换行符 . 当我保存文件时(如下所示),它会将所有内容保存到RTF中的一行 . 您可以在下面看到它的保存方式 .

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 );
    }
}

如你所见,我尝试了两种不同的方式 . (一个被注释掉)这些都不起作用,当RichTextBox中的所有内容都在多行上时,它们都只保存一行 .

那么如何才能将文件保存到多行?任何提示或建议将不胜感激 .

注意:保存为.txt文件时,它会正确保存到多行 . 但是,我无法保存为.txt,因为需要保留RichTextBox的颜色和字体 .

编辑:这是RichTextBox的样子 .
RichTextBox Example

保存文件后,当我打开单词RTF时它看起来像这样 .
Bad Output

这是我想要输出的 .
enter image description here

编辑2:添加RTF代码

{\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}
}
}

从它的外观来看,它不是正确编码\ par或\ lines .

3 回答

  • 0

    如果您提供一些您尝试保存的rtf代码示例,您拥有的结果以及您想要的结果,将会更简单地回答 .

    我不确定你想要多行 . rtf是一种更简单的“阅读”格式的人类? richtextbox中的一行是rtf格式的一行,如下所示:

    {\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}
    }
    }
    

    在richtextbox中,我看到了这一点

    aaaa
    bbbb
    
    cccc
    

    我不确定这样做的兴趣,因为换行符由 \par 为段落结尾编码, \line 为当前段落中的新行编码然后当您用程序打开rtf文件时(例如MS Word)新行被尊重 .

    我也是这样得到的:

    {\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

    不要直接添加rtf代码,使用Wpf Paragraph等 .

    当您将新行添加到RichTextBox使用时

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

    而不是\ r \ n或类似的东西 .

  • 0

    正如您在post中看到的那样,如果您将文本作为明文programaticaly添加到RichTextBox控件,则可能必须添加此unicode行分隔符 \u2028 . 它将在Rtf保存的文件中正常转换为 \line .

    我建议你使用TextRange的方法 .

相关问题