打印列表<> to PrintDocument - 显示前37个项目,然后显示下一个37项目,依此类推

时间:2014-06-09 07:55:06

标签: c# winforms visual-studio printing

目标:打印出读入我的应用程序并存储在列表中的记录<>像这样http://www.codeproject.com/Tips/733680/Printing-and-Previewing-multiple-pages-in-Csharphttps://stackoverflow.com/a/11067893/2952390。更多,所以我想打印出第一页上的前37个记录(如果有那么多),然后是第二页上的下一个37记录,依此类推。该信息显示在我的PrintDocument中包含许多列的基本表中。我最多可以有22500条记录!例如,如果我有104条记录,我需要3页,总体布局应如下所示:

enter image description here

以下代码显示所有页面上的所有记录,但重叠。我可以看到代码已经设法将记录分成一次显示37位于正确的位置,但页面不清除所以它只是重叠。 if语句也可能不正确,因为我不确定它是仅显示74条记录还是整条104条记录。

int currentpage = 0;
int pagesleft = 0;
readonly PrintDocument docccc = new PrintDocument();
readonly PrintPreviewDialog printpreviewdialog = new PrintPreviewDialog();

    private void Summary_Preview(object sender, EventArgs e)
    {
        printpreviewdialog.Document = docccc;
        docccc.DefaultPageSettings.Landscape = true;
        docccc.PrintPage += new PrintPageEventHandler(Summary_English);
        printpreviewdialog.ShowDialog();
    }

    private void Summary_Print(object sender, EventArgs e)
    {
        docccc.PrintPage += new PrintPageEventHandler(Summary_English);
        printDialog1.Document = docccc;
        docccc.DefaultPageSettings.Landscape = true;
        printDialog1.ShowDialog();

        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            docccc.Print();
        }
    }

private void Summary_English(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    // Title
    // Extra text
    // Table headers

    int rec_count = Details.rec_num.Count; // Counts the total records from file
    int pageCount = (rec_count + 37 - 1) / 37; // Calculates how many pages there should be
    currentpage = currentpage + 1;

    if (currentpage == 1)
    {
        pagesleft = pageCount;
    }

    e.Graphics.DrawString(currentpage.ToString() + " | " + pageCount.ToString(), new Font("Courier New", 8, FontStyle.Bold), Brushes.Gray,
         new System.Drawing.Rectangle(22, 780, 1125, 15), Align_Centre);

    int rec = 1; // Initiate record to read
    int y = 257; // Initiate text position

    for (; rec < rec_count; rec++)
    {
        e.Graphics.DrawString(Details.rec_num[rec - 1] + "\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(22, y, 32, 485));
        e.Graphics.DrawString(Details.datetime[rec] + "\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(54, y, 96, 485));
        e.Graphics.DrawString(Details.volt[rec] + "V\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(240, y, 40, 485));
        e.Graphics.DrawString(Details.expectedAtime[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(394, y, 68, 485));
        e.Graphics.DrawString(Details.actualtime1[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(462, y, 102, 485));
        e.Graphics.DrawString(Details.expectedBtime[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(564, y, 69, 485));
        e.Graphics.DrawString(Details.actualtime2[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(633, y, 102, 485));
        e.Graphics.DrawString(Details.error[rec] + "\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(735, y, 92, 485));

        y += 13; // Increment line gap between each rec

        if (rec % 37 == 0)
        {
            y = 257;
        }

        if (currentpage < pageCount)
        { 
            e.HasMorePages = true;              
        }
        else
        {
            e.HasMorePages = false;
        }
    }
}

enter image description here

1 个答案:

答案 0 :(得分:0)

我修好了!!!好极了!很高兴。对于那些处于我的位置并且苦苦挣扎的人:

int currentpage = 0;
int pagesleft = 0;
int rec = 0;

private void Summary_English(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    rec = rec += 1;

    // Title
    // Extra text
    // Table headers

    int rec_count = Details.rec_num.Count; // Counts the total records from file
    int pageCount = (rec_count + 37 - 1) / 37; // Calculates how many pages there should be
    int y = 257;
    currentpage = currentpage + 1;

    if (currentpage == 1)
    {
        pagesleft = pageCount - 1;
    }

    for (; rec < rec_count; rec++)
    {
        e.Graphics.DrawString(Details.rec_num[rec - 1] + "\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(22, y, 32, 485));
        e.Graphics.DrawString(Details.datetime[rec] + "\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(54, y, 96, 485));
        e.Graphics.DrawString(Details.volt[rec] + "V\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(240, y, 40, 485));
        e.Graphics.DrawString(Details.expectedAtime[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(394, y, 68, 485));
        e.Graphics.DrawString(Details.actualtime1[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(462, y, 102, 485));
        e.Graphics.DrawString(Details.expectedBtime[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(564, y, 69, 485));
        e.Graphics.DrawString(Details.actualtime2[rec] + " S\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(633, y, 102, 485));
        e.Graphics.DrawString(Details.error[rec] + "\n", ar_7_reg, Brushes.Black, new System.Drawing.Rectangle(735, y, 92, 485));

        y += 13;

        if (rec % 37 == 0)
        {
            y = 257;
            break;
        }
    }

    e.HasMorePages = currentpage < pageCount;
}

没有我最初预期的代码!