簡體   English   中英

C# 從 MemoryStream 打開 Office 文檔和 Xps 文件

[英]C# Open Office Documents and Xps Files from MemoryStream

我有一個通過 Xps 查看器查看 Word 和 Excel 文件的應用程序。 我將 office 文件轉換為 xps 文件並在 WPF XPS Document Viewer 中顯示。

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

我想知道是否有任何解決方案可以將 xps 轉換為 memory stream 並在 Xps Viewer 中查看

編輯:

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

在 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();

請顯示一些代碼...

If your XPS document is written into a stream you could pass that stream to Package.Open then the Package to XpsDocument then the Xpsdocument to DocumentViewer ... this way the xps stays in memory all the time.

例如 Aspose.Words/.Cells 可以從 word 和 Excel 生成 XPS 到 stream - 所以不涉及文件...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM