简体   繁体   English

如何从字节数组创建 XpsDocument?

[英]How to create an XpsDocument from byte array?

I would like to create a new System.Windows.Xps.Packaging.XpsDocument object from byte array, as I will not want to store it immediately on a local machine.我想从字节数组创建一个新的 System.Windows.Xps.Packaging.XpsDocument object,因为我不想立即将它存储在本地机器上。

By using a temp file it works fine:通过使用临时文件,它可以正常工作:

public static XpsDocument OpenXpsDocument(string url)
{
    WebClient webClient = new System.Net.WebClient();
    byte[] data = webClient.DownloadData(url);

    using (BinaryWriter writer = new System.IO.BinaryWriter(File.OpenWrite(xpsTempFilePath)))
    {
        writer.Write(data);
        writer.Flush();
    }

    XpsDocument xpsDocument = new System.Windows.Xps.Packaging.XpsDocument(xpsTempFilePath, FileAccess.Read);
    return xpsDocument;
}

However, what I want to accomplish is more like this:但是,我想要完成的更像是这样的:

public static XpsDocument OpenXpsDocument(string url)
{
    WebClient webClient = new WebClient();
    byte[] data = webClient.DownloadData(url);
    Package package;
    using (Stream stream = new MemoryStream(data))
    {
        package = System.IO.Packaging.Package.Open(stream);
    }
    XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.SuperFast, url);
    return xpsDocument;
}

Usage of the aforementioned methods goes like that:上述方法的使用是这样的:

XpsDocument xps = TaxReturnCreator.OpenXpsDocument(tempFileUrl);
documentViewer1.Document = xps.GetFixedDocumentSequence();

And, using the last-described method of trying to display the XPS content in a WPF window (without saving) crashes with a System.ObjectDisposedException ("Cannot access a closed Stream") (First method works fine).并且,使用最后描述的方法尝试在 WPF window 中显示 XPS 内容(不保存)会导致 System.ObjectDisposedException(“无法访问已关闭的流”)崩溃(第一种方法工作正常)。

Am I supposed to still keep the Stream open after creating the XpsDocument or am I missing something else?在创建 XpsDocument 之后,我是否应该仍然保持 Stream 处于打开状态,还是我错过了其他东西? Maybe someone knows a completely different / better method of retrieving XPS data as bytes over network and creating an XpsDocument object from the data?也许有人知道通过网络将 XPS 数据作为字节检索并从数据创建 XpsDocument object 的完全不同/更好的方法?

You cannot close a stream backing an XpsDocument.您不能关闭支持 XpsDocument 的 stream。 You must allow the Package to manage the backing MemoryStream, which will be collected once this Package is collected.您必须允许 Package 管理后备 MemoryStream,一旦收集到此 Package,就会收集该内存流。 It may seem a bit of a heresy to do the following:执行以下操作似乎有点异端:

public static XpsDocument OpenXpsDocument(string url)
{
    var webClient = new WebClient();
    var data = webClient.DownloadData(url);
    var package = System.IO.Packaging.Package.Open(new MemoryStream(data));
    var xpsDocument = new XpsDocument(package, 
                                      CompressionOption.SuperFast, 
                                      url);
    return xpsDocument;
}

but it is how this must be done.但必须这样做。

You should try including你应该尝试包括

XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.SuperFast, url);

into using block, ie进入using块,即

Package package;
using (Stream stream = new MemoryStream(data))
{
    package = System.IO.Packaging.Package.Open(stream);
    XpsDocument xpsDocument = new XpsDocument(package, CompressionOption.SuperFast, url);
}
return xpsDocument;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM