如何在 WPF 应用程序中生成 FlowDocument 的“打印预览”?

2024-01-09

我的各种 WPF 应用程序显示 FlowDocument。我可以使用中描述的方法打印它们的答案打印 WPF FlowDocument https://stackoverflow.com/questions/345009/printing-a-wpf-flowdocument/853461#853461.

现在我想添加“打印预览”功能。在正常情况下,我正在打印窗口中显示的 FlowDocument,因此我不需要打印预览。但在某些情况下,要打印的 FlowDocument 是在内存中动态构建的。在这些情况下,我想在打印之前显示它。

现在,我当然可以弹出一个新窗口并显示 FlowDocument,但是

  1. 我希望预览真的feel就像它是打印操作的一部分,而不仅仅是应用程序中的另一个窗口。

  2. 我不想在 FlowDocumentScrollViewer 中使用普通的 FlowDocument。它不是“任何尺寸”,而是需要受到纸张尺寸、特定高宽比和分页的限制。

建议?

  • 我应该只使用标准窗口吗?在这种情况下,如何确保 FlowDocument 处于正确的比例?

  • 是否有一种更“集成”的方法可以在属于 Windows 的 PrintDialog UI 范围内进行预览?

Thanks


根据添加到我的问题的评论的提示,我这样做了:

private string _previewWindowXaml =
    @"<Window
        xmlns                 ='http://schemas.microsoft.com/netfx/2007/xaml/presentation'
        xmlns:x               ='http://schemas.microsoft.com/winfx/2006/xaml'
        Title                 ='Print Preview - @@TITLE'
        Height                ='200'
        Width                 ='300'
        WindowStartupLocation ='CenterOwner'>
        <DocumentViewer Name='dv1'/>
     </Window>";

internal void DoPreview(string title)
{
    string fileName = System.IO.Path.GetRandomFileName();
    FlowDocumentScrollViewer visual = (FlowDocumentScrollViewer)(_parent.FindName("fdsv1"));
    try
    {
        // write the XPS document
        using (XpsDocument doc = new XpsDocument(fileName, FileAccess.ReadWrite))
        {
            XpsDocumentWriter writer = XpsDocument.CreateXpsDocumentWriter(doc);
            writer.Write(visual);
        }

        // Read the XPS document into a dynamically generated
        // preview Window 
        using (XpsDocument doc = new XpsDocument(fileName, FileAccess.Read))
        {
            FixedDocumentSequence fds = doc.GetFixedDocumentSequence();

            string s = _previewWindowXaml;
            s = s.Replace("@@TITLE", title.Replace("'", "&apos;"));

            using (var reader = new System.Xml.XmlTextReader(new StringReader(s)))
            {
                Window preview = System.Windows.Markup.XamlReader.Load(reader) as Window;

                DocumentViewer dv1 = LogicalTreeHelper.FindLogicalNode(preview, "dv1") as DocumentViewer;
                dv1.Document = fds as IDocumentPaginatorSource;


                preview.ShowDialog();
            }
        }
    }
    finally
    {
        if (File.Exists(fileName))
        {
            try
            {
                File.Delete(fileName);
            }
            catch
            {
            }
        }
    }
} 

它的作用:它实际上将视觉内容打印到 XPS 文档中。然后,它加载“打印的”XPS 文档,并将其显示在一个非常简单的 XAML 文件中,该文件存储为字符串,而不是作为单独的模块,并在运行时动态加载。生成的窗口具有 DocumentViewer 按钮:打印、调整到最大页宽等。

我还添加了一些代码来隐藏搜索框。看这个答案WPF:如何删除 DocumentViewer 中的搜索框? https://stackoverflow.com/questions/2322727/wpf-how-can-i-remove-the-searchbox-in-a-documentviewer/2323275#2323275我是如何做到的。

效果是这样的:

XpsDocument 可以在 ReachFramework dll 中找到,XpsDocumentWriter 可以在 System.Printing dll 中找到,两者都必须添加为项目的引用

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 WPF 应用程序中生成 FlowDocument 的“打印预览”? 的相关文章

随机推荐