使用 XPS 查看器打开保存为 XPS 文档的 FlowDocument?
我正在将 WPF FlowDocument 保存到文件系统,使用此代码和带有 xps 扩展名的文件名:
// Save FlowDocument to file system as XPS document
using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var textRange = new TextRange(m_Text.ContentStart, m_Text.ContentEnd);
textRange.Save(fs, DataFormats.XamlPackage);
}
我的应用程序可以使用此代码重新加载文档:
// Load file
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
m_Text = new FlowDocument();
var textRange = new TextRange(m_Text.ContentStart, m_Text.ContentEnd);
textRange.Load(fs, DataFormats.XamlPackage);
}
但是,Windows 7 附带的 XPS 查看器无法打开这些文件。保存的 XPS 文件显示 XPS 图标,但当我双击其中一个时,XPS 查看器无法打开它。错误消息显示“XPS 查看器无法打开此文档”。
知道我需要对 XPS 文档执行什么操作才能使其可由 XPS 查看器打开吗?感谢您的帮助。
I am saving a WPF FlowDocument to the file system, using this code and a fileName with an xps extension:
// Save FlowDocument to file system as XPS document
using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
var textRange = new TextRange(m_Text.ContentStart, m_Text.ContentEnd);
textRange.Save(fs, DataFormats.XamlPackage);
}
My app can reload the document using this code:
// Load file
using (var fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
{
m_Text = new FlowDocument();
var textRange = new TextRange(m_Text.ContentStart, m_Text.ContentEnd);
textRange.Load(fs, DataFormats.XamlPackage);
}
However, the XPS Viewer that ships with Windows 7 can't open the files. The saved XPS files display the XPS icon, but when I double click one, the XPS viewer fails to open it. The error message reads "The XPS Viewer cannot open this document."
Any idea what I need to do to my XPS document to make it openable by the XPS Viewer? Thanks for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 Michael 所评论的,FlowDocument 与 XPS 文档不同。 FlowDocuments 用于屏幕阅读,并且当窗口大小更改时会自动重排,而 XPS 文档的布局是固定的。
编写 XPS 文档所需的类称为 XpsDocument。您需要引用 ReachFramework.dll 程序集才能使用它。以下是将 FlowDocument 保存到 XPS 文档的方法的简短示例:
冯远在他的博客上有一个更大的示例(包括如何添加页眉和页脚以及重新缩放输出) )。
As Michael commented, a FlowDocument is not the same as an XPS document. FlowDocuments are meant for on-screen reading and will reflow automatically when the window size is changed, while the layout of an XPS document is fixed.
The class you need for writing XPS documents is called XpsDocument. You need to reference the ReachFramework.dll assembly to use it. Here's a short example of a method that saves a FlowDocument to an XPS document:
Feng Yuan has a larger example on his blog (including how to add headers and footers and rescale output).