用 C# 打印 (wpf)

发布于 2024-11-01 19:03:21 字数 171 浏览 0 评论 0原文

我正在制作一个 C# WPF 程序,我的程序必须能够打印发票,但我有点努力找出 WPF 中打印的工作原理...如果我记得很清楚,在 winforms 中进行编程,那么您会使用GDI+打印。但是,我认为 WPF 的情况并非如此。

如果有人能通过一些有用的文档或示例的链接为我指明正确的方向,我将非常高兴......

I'm making a C# WPF program, and my program has to be able to print invoices, but I'm kinda struggling to find out how printing works in WPF... If I remember well from programming in winforms, there you'd use GDI+ to print. However, I assume that's not the case with WPF.

I would be very happy if someone could point me in the right direction with some links to helpful documents or examples...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

挽清梦 2024-11-08 19:03:21

在 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,它也非常好。

Printing in WPF is both simple and not so simple.

It starts with basically with one or two lines of code you are printing already.

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

However, pagination in WPF is not done with a single line of code. Then you get into FlowDocuments and similar more advanced topics.

If you are making a non-commercial tool for yourself, consider iTextSharp which is very good too.

辞旧 2024-11-08 19:03:21

如果要打印 WPF 中 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");
}

如果在执行过程中发生任何错误,请告诉我,我会解决它。只有您复制并过去才能运行代码。

If you want to print all records from Datagrid in WPF. In which I have create flow document using code you can understand logic and make it according to own requirement. After a lot of working. I have done code recently. It is tested code. It will print every datagrid with all records. It is easy and simple code. You would add a class. If you want to decorate a datagrid then go to PrintDG class then decorate it according to own requirement.
Follow these steps.
Step1:Add these references on top.

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

Step2: Add class 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, "");

        }
    }

}

Step2: Then go to print button click event and create object of PrintDG class then call printDG pass to It two parameters datagridname and title.
Like :

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

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

If any erorr occure during execution tell me I will solve It.The is running code only you copy and past.

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