打印大型 WPF 用户控件 [英] Printing large WPF User Controls
问题描述
我有大量数据要使用 WPF 打印.我发现 WPF 提供了一个 PrintDialog.PrintVisual
方法,用于打印从 Visual
类派生的任何 WPF 控件.
I have a huge data which I want to print using WPF. I found that WPF provides a PrintDialog.PrintVisual
method for printing any WPF control derived from the Visual
class.
PrintVisual
只会打印一个页面,所以我需要缩放控件以适应页面.不幸的是,这对我不起作用,因为报告有时太长,以至于在缩放以适合页面时无法轻松阅读.
PrintVisual
will only print a single page so I need to scale the control to fit on the page. Unfortunately this would not work for me since the report was sometimes long enough that it could not be read easily when scaled to fit on the page.
WPF 提供的另一个打印选项是在 FlowDocument
中创建一个单独的视图.这可能是打印文档的最佳方式,但它的工作量比我希望的要多,更不用说必须为我希望打印的每个控件维护的额外视图.
Another option for printing provided by WPF is to create a separate view in a FlowDocument
. This is probably the best way to print documents, but it was more work than I wished to put into it, not to mention the extra view that would have to be maintained for each control I wished to print.
我在这个链接中得到了另一个解决方案,但它对我来说似乎太复杂了.
I got another solution in this link, but it seems too complex for me.
有没有更好更简单的解决方案?感谢您的帮助
Is there any better and simple solution for this ? Thanks for any help
推荐答案
我假设您的报表显示在 DataGrid
或其他可滚动的内容中?
I'm assuming your report is displayed in a DataGrid
or something else that is scrollable?
我相信 FlowDocument
绝对是您在这里的最佳选择,如果您想打印一些看起来很专业的东西,没有更好的词,专业.但是如果你想要快速而肮脏的东西,你可以使用一系列使用 RenderTargetBitmap.Render
的操作.基本流程是:
I believe FlowDocument
is definitely your best choice here if you want to print something that looks, for lack of a better word, professional. But if you want something quick and dirty, you could use a series of operations using RenderTargetBitmap.Render
. The basic process would be:
- 创建
RenderTargetBitmap
- 滚动视图,使您可以在一页上看到要打印的区域
- 在包含大"控件的
DataGrid
或ScrollViewer
上调用RenderTargetBitmap.Render
- 打印生成的位图
- 重复下一个页面"
同样,不要在大"控件上调用 RenderTargetBitmap.Render
.如果尚未将大控件包装在 ScrollViewer
中.那基本上就是你的分页器.
Again, don't call RenderTargetBitmap.Render
on the "large" control. Wrap the large control in a ScrollViewer
if it isn't already. That will essentially be your paginator.
我不知道你对结果是否满意,但这是我能想到的最简单的方法.看起来您每次都手动点击 PrintScreen
.不确定这是否是您想要的,但如果您想让它看起来更好看,我认为您需要使用 FlowDocument
.
I don't know if you'll be satisfied with the results, but this is the easiest method I can think of. It'll look like you manually hit PrintScreen
each time. Not sure if that's what you want, but if you want it to look nicer, I think you need to use FlowDocument
.
这篇关于打印大型 WPF 用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!