在多个页面上打印c#

时间:2012-12-08 13:14:53

标签: c# winforms printing

我想用c#打印更多页面。在函数printDocument1_PrintPage中打印我有的文件:

 int values = 0;
 foreach (DataRow row in rows.Rows)

            values++;
           if (values > 48)
            {
                e.HasMorePages = true;
                values = 0;
            }
            else
            {
                e.HasMorePages = false;
            }

问题在于它不停地打印页面。如何跟踪页面上要打印的行数?

1 个答案:

答案 0 :(得分:4)

 int values = 0;
 foreach (DataRow row in rows.Rows)

这不起作用,它为每个页面的值启动为0。所以它永远不会停止。您需要在PrintPage方法之外移动变量。你还需要实现BeginPrint,所以你将从0开始.foreach()也很麻烦,你不想在每个页面的开头都重新开始。所以重写它类似于:

    private int PrintRow;

    private void printDocument1_BeginPrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
        PrintRow = 0;
    }

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) {
        for (int lines = 0; lines < 48; ++lines) {
            PrintRow++;
            if (PrintRow >= rows.Rows.Count) return;  // Done printing
            var row = rows.Rows[PrintRow];
            // Print row
            //...
        }
        e.HasMorePages = PrintRow < rows.Rows.Count;
    }