如何打印库存系统的销售发票

时间:2014-02-14 13:00:15

标签: c# wpf printing

我想知道如何打印从WPF应用程序中的数据库表接收的数据。

我为一个晚餐市场做了一个库存系统,但是如果没有完成销售发票打印部分我就无法完成。

你能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您可以使用PrintDialog打印DocumentPaginator的实例。您可以从FlowDocument获得DocumentPaginator,如下所示:

FlowDocument document = ...
flowDocumentPaginator = ((IDocumentPaginatorSource)document).DocumentPaginator;

var dialog = new PrintDialog();
dialog.PrintDocument(flowDocumentPaginator, "Your description");

基本上,您所要做的就是创建一个FlowDocument并使用数据库中的数据为其添加Table

以下是一些代码,可帮助您开始使用FlowDocument

var document = new FlowDocument();
document.PagePadding = new Thickness(20, 60, 20, 20);
document.ColumnGap = 0;
var table = new Table();
table.CellSpacing = 0;

var quantityColumn = new TableColumn();
quantityColumn.Width = new GridLength(80);
var priceColumn = new TableColumn();
priceColumn .Width = new GridLength(80);
var textColumn = new TableColumn();
textColumn.Width = new GridLength(500);

table.Columns.Add(quantityColumn);
table.Columns.Add(priceColumn);
table.Columns.Add(textColumn);

var rowGroup = new TableRowGroup();
table.RowGroups.Add(rowGroup);

foreach (var item in yourData)
{
    //Add your data
    var itemRow = new TableRow();

    //Assuming your data item has Quantity, Price and Text
    itemRow.Cells.Add(new TableCell(new Paragraph(new Run(item.Quantity.ToString()))));
    itemRow.Cells.Add(new TableCell(new Paragraph(new Run(item.Price.ToString()))));
    itemRow.Cells.Add(new TableCell(new Paragraph(new Run(item.Text))));
    //Etc.
}

document.Blocks.Add(table);