【发布时间】:2011-08-05 09:14:41
【问题描述】:
我正在制作一个 C# WPF 程序,我的程序必须能够打印发票,但我有点想知道如何在 WPF 中打印...如果我记得在 winforms 中编程,那里您将使用 GDI+ 进行打印。但是,我认为 WPF 并非如此。
如果有人可以通过一些指向有用文档或示例的链接为我指明正确的方向,我将非常高兴...
【问题讨论】:
我正在制作一个 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 和类似的更高级的主题。
如果您正在为自己制作非商业工具,请考虑iTextSharp,这也非常好。
【讨论】:
【讨论】:
如果您想在 WPF 中打印数据网格中的所有记录。在其中我使用代码创建了流文档,您可以理解逻辑并根据自己的要求进行制作。经过大量工作。我最近做了代码。它是经过测试的代码。它将打印所有记录的每个数据网格。这是简单的代码。您将添加一个类。如果要装饰数据网格,请转到 PrintDG 类,然后根据自己的要求对其进行装饰。
请按照以下步骤操作。
第 1 步:将这些引用添加到顶部。
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
Step2:添加类 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, "");
}
}
}
Step2:然后去打印按钮点击事件并创建PrintDG类的对象然后调用printDG传递给它两个参数datagridname和title。
喜欢:
private void print_button_Click(object sender, RoutedEventArgs e)
{
PrintDG print = new PrintDG();
print.printDG(datagridName, "Title");
}
如果在执行过程中出现任何错误,告诉我我会解决它。正在运行的代码只有你复制和过去。
【讨论】: