WPF 打印 - 通过 Flowdocument、Paginator 和 FixedDocument 的多页发票 [英] WPF Printing - Multipage Invoice Via Flowdocument, Paginator and FixedDocument

查看:105
本文介绍了WPF 打印 - 通过 Flowdocument、Paginator 和 FixedDocument 的多页发票的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们目前正在使用 WPF 创建多页发票文档,然后通过 XPS 打印/导出.

We're currently using WPF to create a multi-page invoice document, to then be printed / exported via XPS.

我们为实现这一目标而采取的方法是创建一个包含标准 ListBox 等的 UserControl,用于显示发票行,然后将其包含在 BlockUIContainer 标签内的 FlowDocument 中.

The route we've taken to achieve this is to create a UserControl containing a standard ListBox etc displaying the Invoice lines, this is then included in a FlowDocument inside BlockUIContainer tags.

当这个 FlowDocument 被放置在一个窗口中的 FlowDocumentScrollViewer 标签内时,它可以完美地工作,并且 UserControl 的数据绑定内容可以正确显示.但是,当我们尝试在代码中创建相同的 FlowDocument 时,它失败并显示'无法创建未知类型'{clr-namespace:FOO}FooUserControl"XamlParseException.如果删除 UserControl,则可以通过编程方式成功创建 FlowDocument.

When this FlowDocument is placed inside FlowDocumentScrollViewer tags in a window, it works perfectly, with the databound content of the UserControl displaying properly. However, when we try to create the same FlowDocument in code, it fails with a "'Cannot create unknown type '{clr-namespace:FOO}FooUserControl" XamlParseException. The FlowDocument can be successfully created programmatically if the UserControl is removed.

这是 FlowDocument XAML:

This is the FlowDocument XAML:

<FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:local="clr-namespace:MARS"
          ColumnWidth="400" FontSize="14" FontFamily="Georgia">
<Paragraph>
    Blah
</Paragraph>

<BlockUIContainer>
    <local:printTestUserControl></local:printTestUserControl>
</BlockUIContainer>

这是我们用来在代码中创建它的代码:

And this is the code we are using to create it in code:

FileStream xamlFile = new FileStream("printTestFlowDoc.xaml", FileMode.Open, FileAccess.Read);            
FlowDocument content = (FlowDocument)XamlReader.Load(xamlFile);
flowDocScrollViewer.Document = content;
xamlFile.Close();

我们在代码中创建 FlowDocument 的原因是然后使用分页器对象将其切片成一系列固定文档,然后打印/导出到 XPS,我们还没有尝试过这些,但是从我'到目前为止,这似乎是在 WPF 中实现多页文档打印的可行方法(文档在第一页上有一个页眉,在最后一页有一个总计页脚,中间有 x 页行).

The reason we're creating the FlowDocument in code is to then use a Paginator object to slice it into a series of FixedDocuments to then print / export to XPS, none of which we've yet tried, but from what i've read so far it seems like this is a feasible method of achieving multi-page document printing in WPF (Where the document will have a header on the first page, a totals footer on the last, and x pages of rows in between).

非常欢迎有关此问题的任何建议或其他方法.

Any advice on this issue, or other methods of doing this would be very welcome.

这些是我们迄今为止收集到的一些信息的一些链接,但遗憾的是还不够!(我将在评论中包含其他链接,因为 StackOverflow 目前不相信我有多个链接!)

These are some links we've picked up bits of information from so far, sadly not enough though! (I'll include the other links in comments, as StackOverflow doesn't trust me with more than one link at present!)

请参阅动态创建 FlowDocument、数据绑定和打印"部分 ScottHanselmann 似乎和我们有同样的问题,但添加了一行

See the section "Dynamically Creating a FlowDocument, Data Binding and Printing It" Scott Hanselmann seemed to have the same problem as we did, but adding the line

Dispatcher.CurrentDispatcher.Invoke(DispatcherPriority.SystemIdle, new DispatcherOperationCallback(delegate { return null; }), null);

没有加载我们的 FlowDocument,但是他只是绑定到 FlowDocument 中的 TextBlocks,而不是包含 UserControl.

didn't get our FlowDocument to load, however he was only binding to TextBlocks in his FlowDocument, rather than including a UserControl.

非常感谢您阅读到这里!以及任何人可以提供的任何帮助.

Thanks very much for reading this far! and for any help anyone can offer.

推荐答案

抱歉,我整理这个问题的速度太慢了,但我们刚刚完成了这个项目,我现在正在整理所有松散的部分来自它.

Sorry i've been so slow in tidying up this question, but we've just finished this project, and i'm now tidying up all the loose ends from it.

事实证明,流程文档/XPS 方法是处理此任务的完全错误的方法,事实上,内置的 RDLC 报告使我们能够以相对直接的方式实现发票文档所需的一切.

It turned out that the flowdocument / XPS method was a completely wrong headed way of approaching this task, and in fact the built in RDLC reports allowed us to achieve everything we needed for our invoice documents, in a relatively straight forward manner.

这样做的主要好处是我们能够报告业务模型对象的内存实例,而不必使用从数据库中查找的数据,通过从 Crystal 调用的存储过程,以前的系统依赖于之上.VS 中的报告文件编辑器易于使用,但有时在布置复杂报告时会有点烦人.

The main bonus of this was that we were able to report on in-memory instances of our business model objects, rather than having to use data looked up from a db, via stored procs called from crystal, which the previous system relied upon. The report file editor in VS was simple to get to grips with, if at times a little irritating when laying out complex reports.

希望这可能对人们在 WPF 应用程序报告时考虑他们的选择有用.

Hopefully this may be of some use to people considering their options when it comes to reporting from WPF applications.

这篇关于WPF 打印 - 通过 Flowdocument、Paginator 和 FixedDocument 的多页发票的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆