Antonio's improved method (note that I defined a code page \ansicpg1250):
public static string PlainTextToRtf(string plainText)
{
if (string.IsNullOrEmpty(plainText))
return "";
string escapedPlainText = plainText.Replace(@"\", @"\\").Replace("{", @"\{").Replace("}", @"\}");
escapedPlainText = EncodeCharacters(escapedPlainText);
string rtf = @"{\rtf1\ansi\ansicpg1250\deff0{\fonttbl\f0\fswiss Helvetica;}\f0\pard ";
rtf += escapedPlainText.Replace(Environment.NewLine, "\\par\r\n ") + ;
rtf += " }";
return rtf;
}
.
Encode characters (Polish ones) method:
private static string EncodeCharacters(string text)
{
if (string.IsNullOrEmpty(text))
return "";
return text
.Replace("ą", @"\'b9")
.Replace("ć", @"\'e6")
.Replace("ę", @"\'ea")
.Replace("ł", @"\'b3")
.Replace("ń", @"\'f1")
.Replace("ó", @"\'f3")
.Replace("ś", @"\'9c")
.Replace("ź", @"\'9f")
.Replace("ż", @"\'bf")
.Replace("Ą", @"\'a5")
.Replace("Ć", @"\'c6")
.Replace("Ę", @"\'ca")
.Replace("Ł", @"\'a3")
.Replace("Ń", @"\'d1")
.Replace("Ó", @"\'d3")
.Replace("Ś", @"\'8c")
.Replace("Ź", @"\'8f")
.Replace("Ż", @"\'af");
}
The issue is when I load up a plain text file the RichTextBox isn't formatting the new lines
you can try to replace\r
with\r\n
(or\n
with\r\n
) before loading to richtextbox – Mongrel