2013-01-02 65 views
0

我有RTF被誤解釋。 StringWriter\t取代RichTextBox無法在表格內處理的某些字符。RichTextBox誤解釋標籤

string rtfBeforeConversion = @"{\rtf1{\trowd\cellx1150 \cellx3750 \cellx7350 Temp\intbl\cell 96 - 99.7\t\intbl\cell 97.9\t\intbl\cell \row}}"; 
string rtfBrokenByConversion = @"{\rtf1{\trowd\cellx1150 \cellx3750 \cellx7350 Temp\intbl\cell 96 - 99.7 \intbl\cell 97.9 \intbl\cell \row}}"; 

rtfBeforeConversion正確顯示3列與一個RichTextBox數據。

rtfBrokenByConversion第三列的結果顯示沒有數據(或根據用於解釋RTF的DLL在其列的右側顯示數據)。

StringWriter的代碼

using (StringWriter sw = new StringWriter()) 
      { 
       GetRTF(sw);//inserts value of rtfBeforeConversion 
       return sw.ToString();//ToString() creates rtfBrokenByConversion 
      } 

我怎樣才能解決這個問題? (請注意,我試圖StringBuilder並有相同的結果)

+0

「它由'StringWriter'插入」你能分享寫入'StringWriter'的代碼是成爲字符串的東西嗎? – dasblinkenlight

回答

0

像其他特殊字符一樣\r, \n,\t不能在RTF內部很容易地使用。該角色需要轉換爲它的RTF等效。在這種情況下,我創建了一個擴展名來清除格式不正確的文本:

public static string ConvertTabs(this string poorlyFormedRtf) 
    { 
     string unicodeTab = "\u0009"; 
     string result = poorlyFormedRtf.Replace(unicodeTab, "\\tab"); 
     return result; 
    } 

現在一切看起來都很棒。

0

您可以通過查看99.7這樣之後的字符識別特定字符串的字符:

string s = @"{\rtf1{\trowd\cellx1150 \cellx3750 \cellx7350 Temp\intbl\cell 96 - 99.7 \intbl\cell 97.9 \intbl\cell \row}}"; 
char c = s[s.IndexOf("99.7", 0) + 4]; //char you want 
int x = (int) c; 
string unicodeHexCode = x.ToString("X"); //hex code for char you want 

X會給你整unicode字符的值 而unicodeHexCode會給你十六進制值。