5

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.

marseilles84
  • 396
  • 2
  • 7
  • 21
  • 3
    Load to richtextbox and then save as rtf – L.B May 23 '12 at 18:10
  • `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 – L.B May 23 '12 at 18:38

5 Answers5

16

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 Bakula
  • 20,445
  • 6
  • 75
  • 102
3

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");
}
Chris W
  • 1,562
  • 20
  • 27
0

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.
John Melville
  • 3,613
  • 28
  • 30
0

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.

bez9199
  • 15
  • 1
  • 5
0

Here is fully working function:

It covers all special characters (not only Polish ones....)

public static string ConvertToRtf(string text)
{
    // using default template from wiki
    StringBuilder sb = new StringBuilder(@"{\rtf1\ansi\ansicpg1250\deff0{\fonttbl\f0\fswiss Helvetica;}\f0\pard ");
    foreach (char character in text)
    {
        if (character <= 0x7f)
        {
            // escaping rtf characters
            switch (character)
            {
                case '\\':
                case '{':
                case '}':
                    sb.Append('\\');
                    break;
                case '\r':
                    sb.Append("\\par");
                    break;
            }

            sb.Append(character);
        }
        // converting special characters
        else
        {
            sb.Append("\\u" + Convert.ToUInt32(character) + "?");
        }
    }
    sb.Append("}");
    return sb.ToString();
}
Gh61
  • 9,222
  • 4
  • 28
  • 39