在C#(wpf)中打印

30
我正在制作一个C# WPF程序,我的程序需要能够打印发票,但我有点难以找出WPF中的打印方式...如果我没有记错,在WinForms编程中,你会使用GDI+来打印。但是,我认为在WPF中情况并非如此。
如果有人能够给我一些链接到有用文档或示例的指引,我将不胜感激...
3个回答

32

在WPF中打印既简单又不简单。

基于只需一两行代码,您已经可以进行打印。

private void PrintBtn_Click(object sender, RoutedEventArgs e)
{
    PrintDialog printDialog = new PrintDialog();
    if (printDialog.ShowDialog() == true)
    {
    printDialog.PrintVisual(grid, "My First Print Job");
    }
}

然而,在WPF中进行分页并不是一行代码就能完成的。这时你需要了解FlowDocuments和其他更高级的主题。

如果你正在为自己制作非商业工具,请考虑使用iTextSharp,它也非常出色。


2
历史文章可以在这里找到: https://web.archive.org/web/20091025074908/http://www.eggheadcafe.com/tutorials/aspnet/9cbb4841-8677-49e9-a3a8-46031e699b2e/wpf-printing-and-print-pr.aspx 有趣的是,点击“打印机友好版本”可以使其更易读。 - maxp
1
我喜欢第一个打印任务! - Yun CHEN

6

如果您想在WPF中打印Datagrid中的所有记录。我已经编写了使用代码创建流文档的逻辑,您可以理解并根据自己的要求进行调整。经过大量努力,我最近完成了这段代码。这是经过测试的代码,它将打印每个包含所有记录的Datagrid。这是简单易懂的代码。您需要添加一个类。如果您想装饰Datagrid,请转到PrintDG类,然后按照自己的要求进行装饰。
请按照以下步骤操作。
步骤1:在顶部添加这些引用。

using System.Windows;
using System.Data;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;

步骤2:添加PrintDG.cs类。
public class PrintDG
{
    public printDG(DataGrid dataGrid, string title)
    {

        PrintDialog printDialog = new PrintDialog();
        if (printDialog.ShowDialog() == true)
        {
            FlowDocument fd = new FlowDocument();

            Paragraph p = new Paragraph(new Run(title));
            p.FontStyle = dataGrid.FontStyle;
            p.FontFamily = dataGrid.FontFamily;
            p.FontSize = 18;
            fd.Blocks.Add(p);

            Table table = new Table();
            TableRowGroup tableRowGroup = new TableRowGroup();
            TableRow r = new TableRow();
            fd.PageWidth = printDialog.PrintableAreaWidth;
            fd.PageHeight = printDialog.PrintableAreaHeight;
            fd.BringIntoView();

            fd.TextAlignment = TextAlignment.Center;
            fd.ColumnWidth = 500;
            table.CellSpacing = 0;

            var headerList = dataGrid.Columns.Select(e => e.Header.ToString()).ToList();


            for (int j = 0; j < headerList.Count; j++)
            {

                r.Cells.Add(new TableCell(new Paragraph(new Run(headerList[j]))));
                r.Cells[j].ColumnSpan = 4;
                r.Cells[j].Padding = new Thickness(4);

                r.Cells[j].BorderBrush = Brushes.Black;
                r.Cells[j].FontWeight = FontWeights.Bold;
                r.Cells[j].Background = Brushes.DarkGray;
                r.Cells[j].Foreground = Brushes.White;
                r.Cells[j].BorderThickness = new Thickness(1, 1, 1, 1);
            }
            tableRowGroup.Rows.Add(r);
            table.RowGroups.Add(tableRowGroup);
            for (int i = 0; i < dataGrid.Items.Count; i++)
            {

                DataRowView row = (DataRowView)dataGrid.Items.GetItemAt(i);

                table.BorderBrush = Brushes.Gray;
                table.BorderThickness = new Thickness(1, 1, 0, 0);
                table.FontStyle = dataGrid.FontStyle;
                table.FontFamily = dataGrid.FontFamily;
                table.FontSize = 13;
                tableRowGroup = new TableRowGroup();
                r = new TableRow();
                for (int j = 0; j < row.Row.ItemArray.Count(); j++)
                {

                    r.Cells.Add(new TableCell(new Paragraph(new Run(row.Row.ItemArray[j].ToString()))));
                    r.Cells[j].ColumnSpan = 4;
                    r.Cells[j].Padding = new Thickness(4);

                    r.Cells[j].BorderBrush = Brushes.DarkGray;
                    r.Cells[j].BorderThickness = new Thickness(0, 0, 1, 1);
                }

                tableRowGroup.Rows.Add(r);
                table.RowGroups.Add(tableRowGroup);

            }
            fd.Blocks.Add(table);

            printDialog.PrintDocument(((IDocumentPaginatorSource)fd).DocumentPaginator, "");

        }
    }

}

步骤2:接着进入打印按钮的点击事件,创建PrintDG类对象,然后调用printDG方法并传入两个参数,datagridname和title。
例如:

private void print_button_Click(object sender, RoutedEventArgs e)
    {
            PrintDG print = new PrintDG();

 print.printDG(datagridName, "Title");
}

如果在执行过程中出现任何错误,请告诉我,我会解决它。这是正在运行的代码,您只需复制并粘贴即可。


虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。如果链接页面更改,仅有链接的答案可能会失效。-【来自审查】 - EJoshuaS - Stand with Ukraine
这个答案很有用,为什么你要打负面评价呢?如果在执行过程中出现任何错误,请告诉我,我会解决它。你只需要复制粘贴即可。 - Muhammad Mehdi

2

不确定为什么它有负面评价,是因为它只有指向源代码的链接而没有摘要吗? - Bek Raupov
4
最后两个链接已不再可用。 - honzakuzel1989
2
@BekRaupov,仅发布链接是不被赞同的,因为它们可能在未来失效,那么答案就变得毫无意义了。(就在我发布这篇文章时,最后一个链接已经失效了...) - Peter

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接