渲染到 XPS 时如何创建带有页眉和页脚的 Xaml FlowDocument?
我正在寻找一种干净通用的方法来描述 XAML FlowDocument 中重复的页眉和页脚,而无需任何代码隐藏。它只需要在从 C# 渲染到 XPS 时正确显示。
I am looking for an idea of a clean generic way to describe repeating page headers and footers in a XAML FlowDocument without any code behind. It only needs to show up correctly when rendered to XPS from C#.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几个月前我遇到了同样的问题,发现这些链接非常有帮助:
WPF 多页报表第四部分分页
http://www.codeproject.com/KB/WPF/PimpedDocumentPaginator.aspx
我使用的基本技术是通过从
DynamicDocumentPaginator
派生来创建自定义分页器,如下所示:在我的例子中,
THeaderFooterModel
和TFooterModel
是PageNumberModel
类型,因为我需要页眉或页脚能够显示当前页码。自定义分页器委托原始 XPS 分页器来完成其大部分工作,因此它将其存储在构造函数中。
THeaderModel
和TFooterModel
类型允许分页器检索每种类型的 XAMLDataTemplates
,这允许您指定页眉和页眉的布局。 XAML 中的页脚,无需求助于自定义绘图代码。在我的代码中,页眉和页脚具有固定大小,因此当创建分页器时,它会检索页眉和页脚模板以确定要保留多少空间。
在提供的链接中的示例代码中,他们用来为页眉和页脚保留空间的技术是使用缩放变换来缩小原始内容。相反,我告诉原始分页器使用减小的页面大小,然后将原始分页器生成的页面添加到
ContainerVisual
并设置其Offset
。如果页眉和页脚的大小是动态的,您可能无法执行此操作,因为页数会不断变化。我记得的唯一的其他复杂之处是,在添加页眉和页脚时需要使用 Dispatcher 队列(请参阅下面的 AddHeaderOrFooterToContainerAsync)。否则数据绑定不起作用。我们稍微颠覆了 WPF 渲染模型以使其发挥作用。
如果不包含代码,这一切都很难解释,因此我在下面附加了自定义渲染器代码。我已经删除了一些不相关的内容,因此如果它不能按原样编译,这可能就是原因:-)
请注意,传入页码偏移量是因为我们的 XPS 文档由多个 FlowDocument 部分组成,并且调用代码会跟踪当前总页码。
希望这有帮助!
I had the same problem a few months ago, and found these links very helpful:
WPF Multipage Reports Part IV Pagination
http://www.codeproject.com/KB/WPF/PimpedDocumentPaginator.aspx
The basic technique I used was to create a custom paginator by deriving from
DynamicDocumentPaginator
as follows:In my case,
THeaderFooterModel
andTFooterModel
are subclasses of aPageNumberModel
type as I needed the header or footer to be able to show the current page number.The custom paginator delegates to the original XPS paginator to do the majority of its work, so it stores it away in the constructor.
The
THeaderModel
andTFooterModel
types allow the paginator to retrieve XAMLDataTemplates
for each type, which is what allows you to specify the layout of the header and footer in XAML without resorting to custom drawing code.In my code, the header and footer are of a fixed size, so when the paginator is created it retrieves the header and footer templates to determine how much space to reserve.
In the example code in the links provided, the technique they use to reserve space for the header and footer is to use a scale transform to shrink the original content. Instead, I tell the original paginator to use a reduced page size and then add the page the original paginator generates to a
ContainerVisual
and set itsOffset
. You probably couldn't do this if the size of the headers and footers was dynamic because the page count would keep changing.The only other complication I can recall was that you need to use the
Dispatcher
queue when adding headers and footers (seeAddHeaderOrFooterToContainerAsync
below). Data binding doesn't work otherwise. We are slightly subverting the WPF rendering model to get this to work.This would all be quite hard to explain without including the code so I've attached the custom renderer code below. I've stripped out some irrelevant stuff so if it doesn't compile as is that's probably why :-)
Note that the page number offset is passed in because our XPS document is comprised of multiple FlowDocument sections and the calling code keeps track of the current overall page number.
Hope this helps!