How to convert a string to RTF in C#?

前端 未结 7 1635
小鲜肉
小鲜肉 2020-11-29 09:16

Question

How do I convert the string \"Européen\" to the RTF-formatted string \"Europ\\\'e9en\"?

[TestMethod]
public void Convert_A_         


        
相关标签:
7条回答
  • 2020-11-29 09:58

    This is how I went:

    private string ConvertString2RTF(string input)
    {
        //first take care of special RTF chars
        StringBuilder backslashed = new StringBuilder(input);
        backslashed.Replace(@"\", @"\\");
        backslashed.Replace(@"{", @"\{");
        backslashed.Replace(@"}", @"\}");
    
        //then convert the string char by char
        StringBuilder sb = new StringBuilder();
        foreach (char character in backslashed.ToString())
        {
            if (character <= 0x7f)
                sb.Append(character);
            else
                sb.Append("\\u" + Convert.ToUInt32(character) + "?");
        }
        return sb.ToString();
    }
    

    I think using a RichTextBox is:
    1) overkill
    2) I don't like RichTextBox after spending days of trying to make it work with an RTF document created in Word.

    0 讨论(0)
  • 2020-11-29 10:00

    I know it has been a while, hope this helps..

    This code is working for me after trying every conversion code I could put my hands on:

    titleText and contentText are simple text filled in a regular TextBox

    var rtb = new RichTextBox();
    rtb.AppendText(titleText)
    rtb.AppendText(Environment.NewLine);
    rtb.AppendText(contentText)
    
    rtb.Refresh();
    

    rtb.rtf now holds the rtf text.

    The following code will save the rtf text and allow you to open the file, edit it and than load it back into a RichTextBox back again:

    rtb.SaveFile(path, RichTextBoxStreamType.RichText);
    
    0 讨论(0)
  • 2020-11-29 10:04

    I found a nice solution that actually uses the RichTextBox itself to do the conversion:

    private static string FormatAsRTF(string DirtyText)
    {
        System.Windows.Forms.RichTextBox rtf = new System.Windows.Forms.RichTextBox();
        rtf.Text = DirtyText;
        return rtf.Rtf;
    }
    

    http://www.baltimoreconsulting.com/blog/development/easily-convert-a-string-to-rtf-in-net/

    0 讨论(0)
  • 2020-11-29 10:09

    Doesn't RichTextBox always have the same header/footer? You could just read the content based on off-set location, and continue using it to parse. (I think? please correct me if I'm wrong)

    There are libraries available, but I've never had good luck with them personally (though always just found another method before fully exhausting the possibilities). In addition, most of the better ones are usually include a nominal fee.


    EDIT
    Kind of a hack, but this should get you through what you need to get through (I hope):

    RichTextBox rich = new RichTextBox();
    Console.Write(rich.Rtf);
    
    String[] words = { "Européen", "Apple", "Carrot", "Touché", "Résumé", "A Européen eating an apple while writing his Résumé, Touché!" };
    foreach (String word in words)
    {
        rich.Text = word;
        Int32 offset = rich.Rtf.IndexOf(@"\f0\fs17") + 8;
        Int32 len = rich.Rtf.LastIndexOf(@"\par") - offset;
        Console.WriteLine("{0,-15} : {1}", word, rich.Rtf.Substring(offset, len).Trim());
    }
    

    EDIT 2

    The breakdown of the codes RTF control code are as follows:

    • Header
      • \f0 - Use the 0-index font (first font in the list, which is typically Microsoft Sans Serif (noted in the font table in the header: {\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}))
      • \fs17 - Font formatting, specify the size is 17 (17 being in half-points)
    • Footer
      • \par is specifying that it's the end of a paragraph.

    Hopefully that clears some things up. ;-)

    0 讨论(0)
  • 2020-11-29 10:10

    Here's improved @Vladislav Zalesak's answer:

    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();
    }
    
    0 讨论(0)
  • 2020-11-29 10:15

    Not the most elegant, but quite optimal and fast method:

    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");
    }
    
    0 讨论(0)
提交回复
热议问题