在 WPF 中打印集合
有没有办法在 WPF 中以内存集合或可变大小进行打印?
我正在使用以下代码来打印 ListView 控件。 但是,当内容大于垂直滚动条时,垂直滚动条就会接管并剪切内容。
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
printDialog.PrintVisual(lvDocumentSummary, "testing printing!");
Is there any way to print in memory collection or variable size in WPF?
I am using the following code in which I print the ListView control. But when the content is larger than the vertical scroll bar takes over and cuts the content.
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
printDialog.PrintVisual(lvDocumentSummary, "testing printing!");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要打印多页,您只需要使用一个实现 DocumentPaginator 的类,FixedDocument 是较复杂的实现之一,FlowDocument 是较简单的实现之一。
或者
To print multiple pages you just need to use a class that implements DocumentPaginator FixedDocument is one of the more complex implementations, FlowDocument is a simpler one.
or
像任何其他 xaml 文档一样,FixedDocument 支持数据绑定(FlowDocument 除外)。 只需将列表视图托管在固定文档中并将其显示在文档查看器(具有内置打印支持)中。
但是,如果您的列表对于一页而言太长,FixedDocument 不会自动生成新页面(如 flowdocument 那样)。 因此,您必须使用代码手动创建一个新页面,因为这无法在纯 xaml 中完成。
FixedDocument supports DataBinding (other than FlowDocument) like any other xaml document. just host the listview in a fixeddocument and display it in a DocumentViewer (which has built-in print support).
however, if your list is too long for one page, FixedDocument does not automatically generate a new page (like flowdocument does). therefore you have to create a new page maually with code, as this cannot be done in pure xaml.
如果您想从 WPF 中进行良好的打印,您需要构建一个固定文档并打印它,不幸的是,它可能非常复杂,具体取决于您要打印的内容。
这里有一些创建固定文档的示例代码: http://www.ericsink.com/wpf3d/ B_Printing.html
If you want nice printing from WPF you need to build a FixedDocument and print that, unfortunately it can be very complex depending on what you are trying to print.
There's some example code that creates a FixedDocument here: http://www.ericsink.com/wpf3d/B_Printing.html
这是 2019 年的答案。 一些旧的答案不再有效,例如。 FlowDocumentReader 没有
Print
方法。Here's a 2019 answer. Some of the old answers don't work anymore, eg. FlowDocumentReader doesn't have a
Print
method.有趣的是,ListView 是虚拟化的吗? 如果是,则有可能未绘制该对象。 查看 Petzold 的打印示例。
Interesting, Is the ListView virtualized? If it is, the object are not drawn, that is a possibility. Take a look at the Printing example from Petzold.
这是我对这个问题的解决方案。 它有点不稳定,但适用于我的场景。
我阅读了我的收藏并将其转换为字符串。 整个集合现在驻留在 StringBuilder 对象中。 接下来,我将文本/字符串写入客户端计算机上的文件中,然后使用 /p 运行记事本进程来打印文件的内容。
它可以工作并成功打印内容。
最后,有一个计时器在 5 秒后调用并删除文件。 基本上在 5 秒内请求就已经发送到打印机队列。 但更好的解决方案是确保打印作业已得到处理,这样您就可以 100% 确定作业已执行。
Here is my solution to this problem. It is kinda shaky but works for my scenario.
I read my collection and transform it into a string. The whole collection now resides in a StringBuilder object. Next, I saw the text/string into a file on the client's machine and then run the notepad process with /p to print the contents of the file.
It works and it prints the contents successfully.
Finally, there is a timer which is called after 5 seconds and which removes the file. Basically within 5 seconds the request is already sent to the printer queue. But a better solution will be to make sure that the print job has been processed this way you will be 100% sure that the job has been performed.