【问题标题】:printing on a document c# .net在文档 c# .net 上打印
【发布时间】:2016-05-22 07:51:27
【问题描述】:

我正在尝试从我父亲的业务申请中打印发票,我正在使用以下代码打印发票。

我的尝试如下:

{

int y = 470;
        while (i < dataGridView1.RowCount)
        {    
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y));
            e.Graphics.DrawString(dataGridView1.Rows[i].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
            y = y + 20;
            if (y >= pageHeight)
            {

                e.HasMorePages = true;
                y = 0;
                i++;
                return;
           }
            else
            {
                e.HasMorePages = false;
            }
            i++;
        }
}

这里是一个全局变量

private int i = 0;

当我点击预览按钮时,我得到了预期的输出,但是当我在纸上打印它时,只有 while 循环中的内容没有打印出来。我尝试使用局部变量而不是全局变量 j,如下所示,它起作用了。

for (int j=0; j < dataGridView1.RowCount; j++)
        {
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(250, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(600, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(700, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(650, y));
            y = y + 20;
        }

但如果要包含的项目超过页面高度,我不知道如何添加新页面。请帮忙。

【问题讨论】:

  • 为了避免本质上只有链接的答案,dreamincode 有一篇关于将多页打印到打印机的好文章。 MSDN 也有一个不错的例子,如果你会说微软的话。

标签: c# .net wpf printing


【解决方案1】:

在逐步迭代代码后,我发现代码 tp print 预览在打印表单时再次运行。所以我只需要将 i 值初始化为 0 以便它从头开始打印。

这是解决它的代码

int j;
        for (j=i; j < dataGridView1.RowCount && y<e.MarginBounds.Bottom; j++)
        {
            e.Graphics.DrawString(e.MarginBounds.Bottom.ToString(), DefaultFont, Brushes.Black, new Point(100, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["ProductName"].Value.ToString(), DefaultFont, Brushes.Black, new Point(35, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Desc"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(220, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Quantity"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(770, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["UnitPrice"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(620, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Tax"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(670, y));
            e.Graphics.DrawString(dataGridView1.Rows[j].Cells["Total"].Value.ToString(), new Font("Arial", 15, FontStyle.Bold), Brushes.Black, new Point(720, y));
            y = y + 20;
        }
        if (j < dataGridView1.RowCount)
        {
            e.HasMorePages = true;
            i=j;
        }
        else
        {
            e.HasMorePages = false;
            i = 0;
        }

【讨论】:

    猜你喜欢
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多