在多页上打印 C# [英] Print on multiple pages c#

查看:68
本文介绍了在多页上打印 C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 c# 打印更多页面.在打印文档的函数 printDocument1_PrintPage 中:

I would like to print more pages using c# . in the function printDocument1_PrintPage which prints the document i have:

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

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

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

The problem is that it keeps printing pages without stop. How can i keep track of how many rows to print on page ?

推荐答案

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

那行不通,每个页面的值都从 0 开始.所以它永远不会停止.您需要将变量移到 PrintPage 方法之外.您还需要实现 BeginPrint,以便从 0 重新开始. foreach() 也很麻烦,您不想为每一页都从头开始.所以像这样重写它:

That cannot work, it starts values at 0 for every page. So it never stops. You'll need to move the variable outside of the PrintPage method. You'll also need to implement BeginPrint so you'll start back at 0. The foreach() is trouble as well, you don't want to start back at beginning for every page. So rewrite it similar to this:

    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;
    }

这篇关于在多页上打印 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆