Convert txt to rtf

I have a bunch of text files that I want to convert to rtf. Just changing the extension in the code does not work, the main file is the same. I need text in rtf format. Does anyone know how I can do this?

The problem is that I am loading a simple text file that RichTextBox does not format new lines, so it loads it as one continuous block of text, rather than inserting new lines.

The only solution is to open the text file and save as rtf.

+5
source share
4 answers

Just add text to an empty RTF template, plain text does not have any formatting anyway, so let's say the rtf template looks like this (from the example to Wikipedia):

{\rtf1\ansi{\fonttbl\f0\fswiss Helvetica;}\f0\pard _TEXT_CONTENT_HERE_ }

: , :)

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

( , \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;
}

.

():

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

Zbignew Wiadro ( ), .

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

.

  • StringBuilder , , 10% - , . ( gess , (.)
  • .
  • , switch , .
  • .
  • output the string builder to a string.
0
source

I found a method that should work. Open a text file (.txt) with TextEdit. Click the Format drop-down list in the upper left menu bar. There should be a button named Make Rich Text. When you click on it, it should format all your text as rich. Switch the button to make it plain text. It also changes the file type to .rtf. If you do not have Windows, this should work with the latest OS.

0
source

All Articles