打印数据表行更多页面问题

时间:2019-03-26 07:24:07

标签: c# printing datatable printdocument printdialog

我需要打印数据表行。我使用打印文档,但无法打印到新页面。例如,我的数据表中有1000行。如果页面上适合25行,将显示40页。 我在屏幕上看到单个页面。 我的代码在这里。

int offsetY = 15;
int startY = 20;
int RowIndex = 0;
int RowCount;

 RowCount = RAPORDT.Rows.Count;

        PrintDocument pd = new PrintDocument();
        pd.OriginAtMargins = true;
        pd.DefaultPageSettings.Landscape = true;
        Margins margins = new Margins(20, 20, 20, 20);
        pd.DefaultPageSettings.Margins = margins;
        pd.PrintPage += new PrintPageEventHandler(this.pdNormal_PrintPage);

        PrintDialog printdlg = new PrintDialog();
        PrintPreviewDialog printPrvDlg = new PrintPreviewDialog();
        printPrvDlg.Document = pd;
        printPrvDlg.ShowDialog();
        printdlg.Document = pd;

private void pdNormal_PrintPage(object sender, PrintPageEventArgs e)
    {
        float pageWidth = e.PageSettings.PrintableArea.Width;
        float pageHeight = e.PageSettings.PrintableArea.Height;
        Font printFontNoBoldVerdanaSatir = new Font("Verdana", 7f);
        Font printFontBoldVerdana = new Font("Verdana", 7f, FontStyle.Bold);
        e.Graphics.DrawString("TARİH", printFontBoldVerdana, Brushes.Black, 10, 20, new StringFormat());
        e.Graphics.DrawString("ÜRÜN", printFontBoldVerdana, Brushes.Black, 80, 20, new StringFormat());
        e.Graphics.DrawString("CARİ", printFontBoldVerdana, Brushes.Black, 400, 20, new StringFormat());
        float fontHeight = printFontNoBoldVerdanaSatir.GetHeight();
        while (RowIndex < RowCount - 1)
        {
            e.HasMorePages = offsetY >= (pageHeight - 50);
            if (e.HasMorePages == true)
            {
                offsetY = 15;
                startY = 20;

            }
         e.Graphics.DrawString(Convert.ToDateTime(RAPORDT.Rows[RowIndex]["TARIH"].ToString()).ToString("dd.MM.yyyy"), printFontNoBoldVerdanaSatir, Brushes.Black, 10, startY + offsetY, new StringFormat());
            e.Graphics.DrawString(RAPORDT.Rows[RowIndex]["STOKADI"].ToString(), printFontNoBoldVerdanaSatir, Brushes.Black, 80, startY + offsetY, new StringFormat());
            e.Graphics.DrawString(RAPORDT.Rows[RowIndex]["CARIUNVAN"].ToString(), printFontNoBoldVerdanaSatir, Brushes.Black, 400, startY + offsetY, new StringFormat());

            RowIndex++;
            offsetY += (int)fontHeight;
        }
    }

0 个答案:

没有答案