2012-03-12 87 views
0

我有一個Word文檔導入到我的項目的資源文件中。將Resouce文件的Word文檔顯示到RichTextBox控件

是否可以提取此文檔並將其顯示在我的應用程序的RichTextBox控件中?

我能夠使用下面的類從我的項目的資源文件中提取字符串和圖像對象。

enter image description here

namespace TestProject 
{ 
    public class Utilities 
    { 
     private static ResourceManager _resource = new ResourceManager("TestProject.Resource1", Assembly.GetExecutingAssembly()); 

     public static string GetString(string name) 
     { 
      return (System.String)(_resource.GetString(name)); 
     } 

     public static Image GetImage(string name) 
     { 
      return (System.Drawing.Image) (_resource.GetObject(name)); 
     } 
    } 
} 
+0

是否將文檔保存爲RTF格式?如果是這樣,您應該能夠將資源值分配給RichTextBox.RTF屬性。如果不是,我嚴重懷疑RichTextBox可以解析其他形式的Word文檔。 – adelphus 2012-03-12 10:54:24

回答

1

RTF格式爲一個字符串,如果你把它添加到資源文件的文件部分,它將與一個屬性包裝它讀取的字符串。

即:

Properties.Resources.YourDocument; 

實現爲:

internal static string YourDocument { 
     get { 
      return ResourceManager.GetString("YourDocument", resourceCulture); 
     } 
    } 

,並返回富文本看起來像這樣:

{\ RTF1 \ ANSI \ ansicpg1252 \ deff0 \ deflang3081 {\ fonttbl {\ f0 \ fnil \ fcharset0 Calibri;}} {\ colortbl; \ red255 \ green255 \ blue0;} {* \ generator Msftedit 5.41.21.2510;} \ viewkind4 \ uc1 \ pard \ sa200 \ sl276 \ slmult1 \ cf1 \ lang9 \ f0 \ fs22 Rich \ cf0,多行文字\ par \ par \ bs \ bs \ fs22 \ par }

離開你只是需要做:

richTextBox1.Rtf = RichTextResource.Properties.Resources.YourDocument 

,它假定該文件實際上被保存爲富文本。單詞doc將顯示爲垃圾。

最後,如果您的資源存儲爲byte [],則需要先轉換爲字符串。即

richTextBox1.Rtf = System.Text.Encoding.UTF8.GetString(bytes), assuming its UTF8 encoded. 
+0

謝謝Steven,會試試這個。 – yonan2236 2012-03-12 11:41:31

+0

我檢查了導入到我的項目的資源文件中的RTF,它是byte []類型。我也沒有使用Visual Studio,所以我使用上面的類手動獲取資源值。 – yonan2236 2012-03-12 11:47:29

+0

也許我只是需要在分配給'richTextBox1.Rtf'之前將這個byte []轉換爲Rtf文檔。不知道雖然... – yonan2236 2012-03-12 11:50:24