converting txt to rtf

两盒软妹~` 提交于 2019-12-03 16:54:50

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

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

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.

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!