[英]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.