WPF:XPSPackagingException

发布于 2024-10-25 04:56:04 字数 681 浏览 6 评论 0原文

我的代码有什么问题。当它尝试覆盖现有的 .xps 文件时,会弹出错误。

在此处输入图像描述

这是我的代码,

                string filename = dlg.FileName;

                XpsDocument xpsDoc = new XpsDocument(filename, FileAccess.ReadWrite);
                XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                FlowDocument flow = (((((chatHistoryPage.LayoutRoot as Grid).Children[7] as ContentControl).Content) as FlowDocumentPageViewer).Document as FlowDocument);

                xpsWriter.Write((flow as IDocumentPaginatorSource).DocumentPaginator);
                xpsDoc.Close();

谢谢

whats wrong with my code. when it tries to overwrite an existing .xps file, error pops.

enter image description here

Here's my code

                string filename = dlg.FileName;

                XpsDocument xpsDoc = new XpsDocument(filename, FileAccess.ReadWrite);
                XpsDocumentWriter xpsWriter = XpsDocument.CreateXpsDocumentWriter(xpsDoc);
                FlowDocument flow = (((((chatHistoryPage.LayoutRoot as Grid).Children[7] as ContentControl).Content) as FlowDocumentPageViewer).Document as FlowDocument);

                xpsWriter.Write((flow as IDocumentPaginatorSource).DocumentPaginator);
                xpsDoc.Close();

Thank you

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

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

发布评论

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

评论(1

初吻给了烟 2024-11-01 04:56:04

XpsDocument xpsDoc = new XpsDocument(filename, FileAccess.ReadWrite); 行并不是打开一个新的空 XPS 文档,而是打开磁盘上现有的文档。正如例外所提到的,该文档已经包含根固定文档序列。为了完全覆盖 XPS 文档,您需要先删除现有的 XPS 文件,然后再尝试在其位置保存新文件。

您最好的选择可能是使用 FileMode 为 Package.Open 进行调用代码>打开或创建|截断,然后将该包提供给 XpsDocument 构造函数的调用。

The line XpsDocument xpsDoc = new XpsDocument(filename, FileAccess.ReadWrite); isn't opening a new, empty XPS document, it's opening the existing one on disk. As the exception mentions, this document already contains a root FixedDocumentSequence. In order to completely overwrite an XPS document, you need to delete the existing XPS file before you attempt to save a new one in its place.

Your best bet is probably to call Package.Open with a FileMode of OpenOrCreate | Truncate, and then feed that package into the call to the XpsDocument constructor.

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