问题
I have a bunch of text files that I want to convert to rtf. Just changing the extension in code doesn't work, the underlying file is the same. I need the text to be in rtf format. Anyone know how I can do this?
The issue is when I load up a plain text file the RichTextBox isn't formatting the new lines so it loads it as one continuous block of text as opposed to inserting the new lines.
The only solution has been to open the plain text file and "Save As" an rtf.
回答1:
Just add text into empty RTF template, plain text doesn't have any formating anyway, so let's say that rtf template looks like this (from wikipedia example):
{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard _TEXT_CONTENT_HERE_ }
Update: I forgot about new lines, braces and backslashes :)
public static string PlainTextToRtf(string plainText)
{
string escapedPlainText = plainText.Replace(@"\", @"\\").Replace("{", @"\{").Replace("}", @"\}");
string rtf = @"{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard ";
rtf += escapedPlainText.Replace(Environment.NewLine, @" \par ");
rtf += " }";
return rtf;
}
回答2:
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");
}
回答3:
A version of Zbignew Wiadro's answer (minus the polish characters) which attempts to avoid multiple string allocations.
public static string Convert(string s)
{
var ret = new StringBuilder((int) (71 + (s.Length * 1.1)));
ret.Append(@"{\rtf1\ansi\ansicpg1250\deff0{\fonttbl\f0\fswiss Helvetica;}\f0\pard ");
foreach (var letter in s)
{
switch (letter)
{
case '\\':
case '{':
case '}':
ret.Append('\\');
break;
case '\r':
ret.Append("\\par");
break;
}
ret.Append(letter);
}
ret.Append(" }");
return ret.ToString();
}
The design is simple.
- Begin with a StringBuilder and guess the final string will include the header, the original text, and a 10% buffer for expansion so we do not have multple array copies as the string builder grows. (If our gess is low, it still will work, at the cost of one array copy (likely.)
- Write out the headder
- Loop through the string and perform all the escaping in a single pass.
- If you wanted to add back the polish conversion, it would just be more cases in the switch statement, not more string copies.
- write out the trailing brace.
- dump the string builder to a string.
回答4:
I found a method that should work. Open the plain text file (.txt) with TextEdit. Click the Format dropdown in the top-left menu bar. There should be a button called Make Rich Text. When you click on that, it should format all of your text as rich. Toggle the button to make it plain text. It also changes the file type to .rtf. Unless you have Windows, this should work with the newest OS.
来源:https://stackoverflow.com/questions/10725438/converting-txt-to-rtf