我使用RichTextBox将RTF中的字符串转换为纯文本,使用以下代码:
private string ConvertToText(string rtf)
{
if (string.IsNullOrWhiteSpace(rtf)) return string.Empty;
if (!rtf.Contains("{\\rtf")) return rtf.Trim();
using (var helper = new System.Windows.Forms.RichTextBox())
{
helper.Rtf = rtf;
var plainText = helper.Text;
if (string.IsNullOrWhiteSpace(plainText)) return string.Empty;
return "<< Rule in Rich Text Format converted to Plain Text >>\n\n"
+ plainText
+ "\n\n<< Rule in Rich Text Format converted to Plain Text >>";
}
}
它适用于所有开发人员计算机,但在部署到Azure网站时不起作用:
{
"$id" :"1",
"Message" :"An error has occurred.",
"ExceptionMessage":"Out of memory.",
"ExceptionType" :"System.OutOfMemoryException",
"StackTrace" :"
at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)\r\n
at System.Drawing.Font.GetHeight()\r\n
at System.Drawing.Font.get_Height()\r\n
at System.Windows.Forms.Control.get_FontHeight()\r\n
at System.Windows.Forms.TextBoxBase.get_PreferredHeight()\r\n
at System.Windows.Forms.TextBoxBase.AdjustHeight(Boolean returnIfAnchored)\r\n
at System.Windows.Forms.TextBoxBase.set_Multiline(Boolean value)\r\n
at System.Windows.Forms.RichTextBox.set_Multiline(Boolean value)\r\n
at System.Windows.Forms.RichTextBox..ctor()\r\n
at ... "
}
无论RTF字符串的大小,都会发生。我已经在使用&#34;复制到本地&#34; System.Windows.Forms
引用的属性。有没有人有处理RTF和Azure的经验?有没有其他方法可以将RTF转换为纯文本?
答案 0 :(得分:1)
这是@Gusman在评论中提出的C++ forum link的改编C#版本:
public static string ConvertToText(string rtf)
{
bool slash = false; //indicates if backslash followed by the space
bool figure_opened = false; //indicates if opening figure brace followed by the space
bool figure_closed = false; //indicates if closing brace followed by the space
bool first_space = false; //the else spaces are in plain text and must be included to the result
if (rtf.Length < 4) return string.Empty;
int i = 0;
i = rtf.IndexOf("\\pard");
if (i < 1) return "";
var builder = new StringBuilder();
for (int j = i; j < rtf.Length - 1; j++)
{
char ch = rtf[j];
char nextCh = rtf[j + 1];
if (ch == '\\' && nextCh == 'p') // appends \n if \pard, except for first
{
if (j > i && j < rtf.Length - 4)
{
string fiveChars = rtf.Substring(j, 5);
if (fiveChars.Equals("\\pard"))
{
builder.Append("\n");
}
}
}
if (ch == '\\' && nextCh == 'u') // to deal correctly with special characters
{
string fourChars = rtf.Substring(j + 2, 4);
string digits = new string(fourChars.TakeWhile(char.IsDigit).ToArray());
char specialChar = (char)int.Parse(digits);
builder.Append(specialChar);
j += digits.Length + 5;
continue;
}
if (ch == '\\' && nextCh == '{') // if the text contains symbol '{'
{
slash = false;
figure_opened = false;
figure_closed = false;
first_space = false;
builder.Append('{');
j++;
continue;
}
else if (ch == '\\' && nextCh == '}') // if the text contains symbol '}'
{
slash = false;
figure_opened = false;
figure_closed = false;
first_space = false;
builder.Append('}');
j++;
continue;
}
else if (ch == '\\' && nextCh == '\\') // if the text contains symbol '\'
{
slash = false;
figure_opened = false;
figure_closed = false;
first_space = false;
builder.Append('\\');
j++;
continue;
}
else if (ch == '\\') // we are looking at the backslash
{
first_space = true;
slash = true;
}
else if (ch == '{')
{
first_space = true;
figure_opened = true;
}
else if (ch == '}')
{
first_space = true;
figure_closed = true;
}
else if (ch == ' ')
{
slash = false;
figure_opened = false;
figure_closed = false;
}
if (!slash && !figure_opened && !figure_closed)
{
if (!first_space)
{
builder.Append(ch);
}
else
{
first_space = false;
}
}
}
return builder.ToString();
}
有效! = d