简体   繁体   English

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

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

I have an application for viewing Word and Excel Files via Xps Viewer.我有一个通过 Xps 查看器查看 Word 和 Excel 文件的应用程序。 I convert office files to xps files and show it in WPF XPS Document Viewer.我将 office 文件转换为 xps 文件并在 WPF XPS Document Viewer 中显示。

But here is a little problem;但这里有一个小问题; I don't want the users see the files, i delete the files after closing.我不希望用户看到文件,我在关闭后删除文件。

I am wondering is there any solution to convert xps in to memory stream and view it in Xps Viewer我想知道是否有任何解决方案可以将 xps 转换为 memory stream 并在 Xps Viewer 中查看

Edit:编辑:

I don't want create any xps file on disk.我不想在磁盘上创建任何 xps 文件。 The convert process must done inside to MemoryStream.转换过程必须在 MemoryStream 内部完成。

Following lines of code worked fine for me on a poc project and can give you a starting point.在 poc 项目中,以下代码行对我来说效果很好,可以为您提供一个起点。 For the document conversion part (word/excel -> xps) you could just print them via automation using XPS Document Writer.对于文档转换部分(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();

please show some code...请显示一些代码...

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. 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.

For example Aspose.Words/.Cells can generate XPS from word and Excel into a stream - so no files involved...例如 Aspose.Words/.Cells 可以从 word 和 Excel 生成 XPS 到 stream - 所以不涉及文件...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一次打开多个实例/文件时,c#保存和关闭办公文档 - c# Saving & Closing office documents, when multiple instances/files open at once 从 MemoryStream 获取 XPS FixedDocumentSequence - Getting XPS FixedDocumentSequence from a MemoryStream c#-来自MemoryStream的DotNetZip打开zip文件 - c# - DotNetZip open zip file from MemoryStream c#将固定文档转换为xps,但是xps查看器将无法打开新创建的xps文件 - c# convert fixed document to xps, but xps viewer will not open the new created xps file 通过C#打开Office Communicator会话 - Open Office Communicator Session from C# C#从.XPS文档中提取文本 - C# Extract Text from .XPS Document 从C#错误打开Office 2010文档 - Opening Office 2010 documents from C# error 如何从C#中的MS office文档中提取文本 - How to extract text from MS office documents in C# 在不打开C#对话框的情况下打印XPS文档 - Print XPS document without open the dialog box in C# 1)如何将Excel写入memorystream…2)如何使用Micosoft.Office.Interop.Excel和c#从浏览器下载Excel文件 - 1) how to write excel to memorystream…2) how to download excel file from browser…using Micosoft.Office.Interop.Excel and c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM