2011-07-20 55 views
2

我有一個通過Xps查看器查看Word和Excel文件的應用程序。我將Office文件轉換爲xps文件並將其顯示在WPF XPS文檔查看器中。C#從MemoryStream中打開Office文檔和Xps文件

但這是一個小問題;我不希望用戶看到這些文件,我在關閉後刪除這些文件。

我想知道有沒有什麼解決XPS轉化到內存流,並查看它在XPS查看器

編輯:

我不想在磁盤上創建任何XPS文件。轉換過程必須在MemoryStream內完成。

回答

7

以下幾行代碼在poc項目中可以很好地工作,並且可以爲您提供一個起點。 對於文檔轉換部分(word/excel - > xps),您可以使用XPS Document Writer通過自動化將其打印出來。

System.IO.Stream docStream = ...any xps as stream; 
Package package = Package.Open(docStream); 

//Create URI for Xps Package 
//Any Uri will actually be fine here. It acts as a place holder for the 
//Uri of the package inside of the PackageStore 
string inMemoryPackageName = string.Format("memorystream://{0}.xps", Guid.NewGuid()); 
Uri packageUri = new Uri(inMemoryPackageName); 

//Add package to PackageStore 
PackageStore.AddPackage(packageUri, package); 

XpsDocument xpsDoc = new XpsDocument(package, CompressionOption.Maximum, inMemoryPackageName); 
FixedDocumentSequence fixedDocumentSequence = xpsDoc.GetFixedDocumentSequence(); 

// Do operations on xpsDoc here 
DocViewer.Document = fixedDocumentSequence; 

//Note: Please note that you must keep the Package object in PackageStore until you 
//are completely done with it since certain operations on XpsDocument can trigger 
//delayed resource loading from the package. 

//PackageStore.RemovePackage(packageUri); 
//xpsDoc.Close(); 
+0

這裏是另一個想法轉換部分http://msdn.microsoft.com /en-us/library/bb238907.aspx –

0

請出示一些代碼...

如果您的XPS文檔寫入流,你可以說流的包傳遞到Package.Open然後XpsDocument那麼XpsdocumentDocumentViewer ...這樣的xps始終保持在內存中。

例如使用Aspose.Words/.Cells可以生成Word和Excel XPS成流 - 因此沒有涉及文件...

+0

我的XPS是在磁盤上的文件。 –

+0

然後通過FileStream加載它或使用一些第三方libarary直接生成XPS到內存流(根本沒有文件)......一個這樣的第三方解決方案是Aspose ...我不附屬,只是一個快樂的客戶。 – Yahia

+0

我不想要付給組件。 –