2014-04-24 3 views
2

나는이 코드 조각을 사용하여 일반 텍스트로 RTF에 문자열을 변환하여 RichTextBox를 사용하고 있습니다에에서 OutOfMemory가 발생합니다 :를 RichTextBox는 푸른

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 >>"; 
    } 
} 

그것은 모든 개발자 기계에서 확인을 작동하지만,이 경우 작동하지 않습니다 푸른 웹 사이트에 배포 :

{ 
"$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 문자열의 발생합니다. 이미 System.Windows.Forms 참조의 "로컬로 복사"속성을 사용하고 있습니다. 누구든지 RTF와 Azure를 다룰 수있는 경험이 있습니까? RTF를 일반 텍스트로 변환 할 수있는 대안이 있습니까?

+0

하늘색 부분에서 더 구체적 일 수 있습니까? 어떤 종류의 하늘색 서비스를 사용하고 있습니까? 가상 기기? 웹 역할? 웹 사이트? –

+0

웹 사이트, Thiago. –

+1

틀릴 수도 있지만 드로잉 컨텍스트가 없으므로 하늘색 웹에서 양식의 컨트롤을 만들 수 없다고 생각됩니다. – Gusman

답변

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