用C#(wpf)打印

用C#(wpf)打印,c#,wpf,printing,C#,Wpf,Printing,我正在制作一个C#WPF程序,我的程序必须能够打印发票,但我正在努力找出WPF中打印是如何工作的。。。如果我在winforms编程中记忆犹新,那么您可以使用GDI+进行打印。然而,我认为WPF的情况并非如此 如果有人能给我指出正确的方向,并提供一些有用文档或示例的链接,我将非常高兴。用WPF打印既简单又不那么简单 它基本上是从你已经打印的一两行代码开始的 private void PrintBtn_Click(object sender, RoutedEventArgs e) { Pri

我正在制作一个C#WPF程序,我的程序必须能够打印发票,但我正在努力找出WPF中打印是如何工作的。。。如果我在winforms编程中记忆犹新,那么您可以使用GDI+进行打印。然而,我认为WPF的情况并非如此


如果有人能给我指出正确的方向,并提供一些有用文档或示例的链接,我将非常高兴。

用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和类似的更高级主题


如果你在为自己制作一个非商业工具,那么考虑哪一个也是非常好的。

< P>这些链接可以帮助你理解打印工作和使用什么:


如果您想在WPF中打印来自datagrid的所有记录。我在其中使用代码创建了流程文档,您可以理解逻辑并根据自己的要求进行制作。经过大量工作。我最近完成了代码。它是经过测试的代码。它将打印所有记录的每个datagrid。这是一个简单而简单的代码。您可以添加一个类。如果想要装饰一个数据网格,然后转到PrintDG类,然后根据自己的要求装饰它。
按照以下步骤操作。
步骤1:在顶部添加这些引用

 using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
步骤2:添加PrintDG.cs类

  public  class PrintDG
{
         public void 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, "");

        }
    }

}

如果在执行过程中出现任何erorr,请告诉我我将解决它。该代码正在运行,只有您复制并通过。

不确定为什么它有负面评级,是因为它只有指向源的链接而没有摘要吗?最后两个链接不再可用。@BekRaupov继续发布链接,因为这些链接将来可能会断开,然后答案就变得毫无意义了。。(当我发布这篇文章时,最后一个链接被破坏了…)历史性的文章可以在这里检索到:有趣的是,点击“打印机友好版”使它更具可读性。我喜欢第一个打印作业!虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-这个答案很有用,为什么你要否定?如果在你执行死刑的过程中发生了什么事,告诉我我会解决的。你只要抄过去就行了。
private void print_button_Click(object sender, RoutedEventArgs e)
    {
            PrintDG print = new PrintDG();

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