使用 XPS 查看器打开保存为 XPS 文档的 FlowDocument?

发布于 2024-08-31 23:32:36 字数 816 浏览 8 评论 0原文

我正在将 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 技术交流群。

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

发布评论

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

评论(1

那些过往 2024-09-07 23:32:36

正如 Michael 所评论的,FlowDocument 与 XPS 文档不同。 FlowDocuments 用于屏幕阅读,并且当窗口大小更改时会自动重排,而 XPS 文档的布局是固定的。

编写 XPS 文档所需的类称为 XpsDocument。您需要引用 ReachFramework.dll 程序集才能使用它。以下是将 FlowDocument 保存到 XPS 文档的方法的简短示例:

using System.IO;
using System.IO.Packaging;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
using System.Windows.Xps.Serialization;

namespace XpsConversion
{
    public static class FlowToXps
    {
        public static void SaveAsXps(string path, FlowDocument document)
        {
            using (Package package = Package.Open(path, FileMode.Create))
            {
                using (var xpsDoc = new XpsDocument(
                    package, System.IO.Packaging.CompressionOption.Maximum))
                {
                    var xpsSm = new XpsSerializationManager(
                        new XpsPackagingPolicy(xpsDoc), false);
                    DocumentPaginator dp = 
                        ((IDocumentPaginatorSource)document).DocumentPaginator;
                    xpsSm.SaveAsXaml(dp);
                }
            }
        }
    }
}

冯远在他的博客上有一个更大的示例(包括如何添加页眉和页脚以及重新缩放输出) )。

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:

using System.IO;
using System.IO.Packaging;
using System.Windows.Documents;
using System.Windows.Xps.Packaging;
using System.Windows.Xps.Serialization;

namespace XpsConversion
{
    public static class FlowToXps
    {
        public static void SaveAsXps(string path, FlowDocument document)
        {
            using (Package package = Package.Open(path, FileMode.Create))
            {
                using (var xpsDoc = new XpsDocument(
                    package, System.IO.Packaging.CompressionOption.Maximum))
                {
                    var xpsSm = new XpsSerializationManager(
                        new XpsPackagingPolicy(xpsDoc), false);
                    DocumentPaginator dp = 
                        ((IDocumentPaginatorSource)document).DocumentPaginator;
                    xpsSm.SaveAsXaml(dp);
                }
            }
        }
    }
}

Feng Yuan has a larger example on his blog (including how to add headers and footers and rescale output).

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