WPF:XPSPackagingException
我的代码有什么问题。当它尝试覆盖现有的 .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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.