我正在制作一个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中打印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;
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");
}
如果在执行过程中出现任何错误,请告诉我,我会解决它。这是正在运行的代码,您只需复制并粘贴即可。
http://www.charlespetzold.com/blog/2006/02/201111.html (archive.org)
http://msdn.microsoft.com/en-us/library/ms742418(v=vs.100).aspx
http://www.switchonthecode.com/tutorials/printing-in-wpf (archive.org)
的英文原文意思是: 在WPF中打印。