从嵌入式资源将 XPS 加载到文档查看器

发布于 2024-12-15 09:40:59 字数 1153 浏览 1 评论 0原文

我正在努力为我的申请提供帮助。我有 xps 文档,正在加载到文档查看器。这些文件嵌入在资源文件中。

我能够以字节数组的形式访问它们。 例如 Properties.Resources.help_sudoku_methods_2 返回 byte[]

但是,文档查看器无法读取它并且需要固定文档序列。 因此,我从 bytearray 创建内存流,然后创建 xpsdocument,然后创建fixeddocumentsequence,如下所示:

 private void loadDocument(byte[] sourceXPS)
        {
            MemoryStream ms = new MemoryStream(sourceXPS);
            const string memoryName = "memorystream://ms.xps";
            Uri memoryUri = new Uri(memoryName);
            try
            {
                PackageStore.RemovePackage(memoryUri);
            }
            catch (Exception)
            { }

            Package package = Package.Open(ms);


            PackageStore.AddPackage(memoryUri, package);

            XpsDocument xps = new XpsDocument(package, CompressionOption.SuperFast, memoryName);

            FixedDocumentSequence fixedDocumentSequence = xps.GetFixedDocumentSequence();
            doc.Document = fixedDocumentSequence;


        }

这是非常不干净的方法,并且如果文件中有图像也不起作用 - 而不是新文档中的图像显示来自第一个加载的文档的图像。

有没有更干净的方法将 XPS 从嵌入式资源加载到文档查看器?或者我是否需要一些想法,例如将文件从资源复制到应用程序目录并从此处加载而不是内存流?谢谢。

i am trying to make help for my application. I have xps documents which i am loading to documentviewer. These files are embedded in resource file.

I am able to access these as bytearray.
For example
Properties.Resources.help_sudoku_methods_2
returns byte[]

However, documentviewer cant read it and requires fixeddocumentsequence.
So i create memory stream from bytearray, then xpsdocument and then fixeddocumentsequence like this:

 private void loadDocument(byte[] sourceXPS)
        {
            MemoryStream ms = new MemoryStream(sourceXPS);
            const string memoryName = "memorystream://ms.xps";
            Uri memoryUri = new Uri(memoryName);
            try
            {
                PackageStore.RemovePackage(memoryUri);
            }
            catch (Exception)
            { }

            Package package = Package.Open(ms);


            PackageStore.AddPackage(memoryUri, package);

            XpsDocument xps = new XpsDocument(package, CompressionOption.SuperFast, memoryName);

            FixedDocumentSequence fixedDocumentSequence = xps.GetFixedDocumentSequence();
            doc.Document = fixedDocumentSequence;


        }

This is very unclean aproach and also doesnt work if there are images in files - instead of images in new documents displays images from first loaded doc.

Is there any cleaner way to load XPS from embedded resources to documentviewer? or do i need somethink like copy file from resources to application directory and load from here and not memorystream? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吻风 2024-12-22 09:40:59

为什么不将文件写入系统临时文件夹,然后从那里读取。

    Stream ReadStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("file1.xps");
        string tempFile = Path.GetTempPath()+"file1.xps"; 
        FileStream WriteStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
        ReadStream.CopyTo(WriteStream);
        WriteStream.Close();
        ReadStream.Close();

        // Read tempFile INTO memory here and then

        File.Delete(tempFile);

why dont you write file to system temp folder and then read from there.

    Stream ReadStream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("file1.xps");
        string tempFile = Path.GetTempPath()+"file1.xps"; 
        FileStream WriteStream = new FileStream(tempFile, FileMode.Create, FileAccess.Write);
        ReadStream.CopyTo(WriteStream);
        WriteStream.Close();
        ReadStream.Close();

        // Read tempFile INTO memory here and then

        File.Delete(tempFile);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文