在 WPF 中打印集合

发布于 2024-07-06 22:56:04 字数 299 浏览 9 评论 0原文

有没有办法在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

有木有妳兜一样 2024-07-13 22:56:04

要打印多页,您只需要使用一个实现 DocumentPaginator 的类,FixedDocument 是较复杂的实现之一,FlowDocument 是较简单的实现之一。

FlowDocument fd = new FlowDocument();

foreach(object item in items)
{
    fd.Blocks.Add(new Paragraph(new Run(item.ToString())));
}

fd.Print();

或者

PrintDialog pd = new PrintDialog();
pd.PrintDocument(fd);

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.

FlowDocument fd = new FlowDocument();

foreach(object item in items)
{
    fd.Blocks.Add(new Paragraph(new Run(item.ToString())));
}

fd.Print();

or

PrintDialog pd = new PrintDialog();
pd.PrintDocument(fd);
ζ澈沫 2024-07-13 22:56:04

像任何其他 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.

巷子口的你 2024-07-13 22:56:04

如果您想从 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

游魂 2024-07-13 22:56:04

这是 2019 年的答案。 一些旧的答案不再有效,例如。 FlowDocumentReader 没有 Print 方法。

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FlowDocument fd = new FlowDocument();
            foreach (var item in COLLECTION) //<- put your collection here
            {
                fd.Blocks.Add(new Paragraph(new Run(item.ToString())));
            }

            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() != true) return;

            fd.PageHeight = pd.PrintableAreaHeight;
            fd.PageWidth = pd.PrintableAreaWidth;

            IDocumentPaginatorSource idocument = fd as IDocumentPaginatorSource;

            pd.PrintDocument(idocument.DocumentPaginator, "Printing Flow Document...");
        }
    }

Here's a 2019 answer. Some of the old answers don't work anymore, eg. FlowDocumentReader doesn't have a Print method.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FlowDocument fd = new FlowDocument();
            foreach (var item in COLLECTION) //<- put your collection here
            {
                fd.Blocks.Add(new Paragraph(new Run(item.ToString())));
            }

            PrintDialog pd = new PrintDialog();
            if (pd.ShowDialog() != true) return;

            fd.PageHeight = pd.PrintableAreaHeight;
            fd.PageWidth = pd.PrintableAreaWidth;

            IDocumentPaginatorSource idocument = fd as IDocumentPaginatorSource;

            pd.PrintDocument(idocument.DocumentPaginator, "Printing Flow Document...");
        }
    }
画骨成沙 2024-07-13 22:56:04

有趣的是,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.

酸甜透明夹心 2024-07-13 22:56:04

这是我对这个问题的解决方案。 它有点不稳定,但适用于我的场景。

我阅读了我的收藏并将其转换为字符串。 整个集合现在驻留在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文