How to convert a string to RTF in C#?

前端 未结 7 1657
小鲜肉
小鲜肉 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.

提交回复
热议问题