C#从MemoryStream打开Office文档和Xps文件

Orh*_*nar 2 .net c# wpf

我有一个通过Xps Viewer查看Word和Excel文件的应用程序.我将office文件转换为xps文件并在WPF XPS Document Viewer中显示它.

但这是一个小问题; 我不希望用户看到文件,我关闭后删除文件.

我想知道是否有任何解决方案将xps转换为内存流并在Xps Viewer中查看它

编辑:

我不想在磁盘上创建任何xps文件.转换过程必须在MemoryStream中完成.

Yiğ*_*ner 8

以下代码行在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();
Run Code Online (Sandbox Code Playgroud)