Exemple #1
0
        private string EncodeText(string text, RtfTextDataType type)
        {
            if (type == RtfTextDataType.Raw)
            {
                return(text);
            }

            if (type == RtfTextDataType.HyperLink)
            {
                return(Uri.EscapeUriString(text));
            }

            StringBuilder encodedText = new StringBuilder();

            byte[] data = _encoding.GetBytes(text);

            for (int i = 0; i < data.Length; i++)
            {
                if (text[i] == Encoding.ASCII.GetChars(data, i, 1)[0])
                {
                    if (text[i] == '\\' || text[i] == '{' || text[i] == '}')
                    {
                        encodedText.Append('\\');
                    }

                    encodedText.Append(text[i]);
                }
                else if (text[i] == _encoding.GetChars(data, i, 1)[0])
                {
                    encodedText.Append('\\').Append('\'').Append(Convert.ToString(data[i], 16));
                }
                else
                {
                    byte[] unicode = Encoding.Unicode.GetBytes(text[i].ToString());
                    encodedText.Append('\\').Append('u').Append(BitConverter.ToUInt16(unicode, 0).ToString()).Append('?');
                }


                if (sb.Length + encodedText.Length - lastNewLineIndex > preferredWidth)
                {
                    encodedText.AppendLine();
                    lastNewLineIndex = sb.Length + encodedText.Length;
                }
            }

            return(encodedText.ToString());
        }
 /// <summary>
 /// Initializes a new instance of ESCommon.Rtf.RtfTextDataAttribute class.
 /// </summary>
 /// <param name="type">Text data type.</param>
 internal RtfTextDataAttribute(RtfTextDataType type)
 {
     _type = type;
 }