无限循环e.HasMorePages在PrintDocument中

时间:2018-08-18 09:58:18

标签: c# printdocument

我正在尝试每页打印特定数量的行,并且在达到最大数量的情况下添加新页面,但是它不能按我预期的那样工作,有时它在第一页上打印4行,有时保持添加新文档页面,对于C#来说,我是一个全新的人,将不胜感激。

foreach (DataTable table in printViewTable) {
    x = 0;
    y += 20;

    foreach (DataRow row in table.Rows) {
        // Check if lines are still in range
        if (lines_drawin < lines_per_page) {
            // Add rows depeding on columns number
            for (int i = table.Columns.Count - 1; i > -1; i--) {
                size = g.MeasureString(row[i].ToString(), font);
                xPadding = (width - size.Width) / 2;

                // Some code to remove duplicate in the print page
                if (i == 0 || i == 1) {
                    if (!sectionsTracker.Contains(row[i].ToString())) {
                        g.DrawString(row[i].ToString(), font, brush, x + xPadding, y + 5 + 30);
                        sectionsTracker.Add(row[i].ToString());
                    }
                }
                else if (i != 0 || i != 1) {
                    g.DrawString(row[i].ToString(), font, brush, x + xPadding, y + 5 + 30);
                }
                x += width;
            }
            // Increment the number of rows is printed on the page
            lines_drawin++;
            x = 0;
            y += 20;
        }
    }
    // Check if the lines are out of range
    // Total of lines variable is the number if rows in each table
    if (lines_drawin + 1 > lines_per_page && total_of_lines > lines_per_page) {
        // Add new page
        e.HasMorePages = true;

        // Multiply the lines per page so the first condition is true and starts adding lines again
        lines_per_page = lines_per_page * 2;
    }
}

0 个答案:

没有答案