如何在打印页面中使用Graphics.Draw?

时间:2012-01-26 22:19:56

标签: c# winforms

我有下一个代码,用于创建一个包含datagridview数据的打印页面。

Font print10B = new Font("Lucida Console", 10, FontStyle.Bold);
Font print8B = new Font("Arial", 9, FontStyle.Regular);
Font print6B = new Font("Arial", 8, FontStyle.Regular);

e.Graphics.DrawString("   NOTA DE PLATA",print10B,Brushes.Black,10,10);
e.Graphics.DrawString("-----------------------------------------", new Font(dataGridView1.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, 10, 70);
e.Graphics.DrawString("Produs   | Cant | Pret | Total ", print8B, Brushes.Black, 10, 100);
int height = 0;
int x = 10;
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
  e.Graphics.DrawString(dataGridView1.Rows[i].Cells["DenumireProdus"].Value.ToString(), print6B, Brushes.Black, x, 120+height);
  e.Graphics.DrawString(dataGridView1.Rows[i].Cells["produs_cantitate"].Value.ToString(), print6B, Brushes.Black, x+70, 120 + height);
  e.Graphics.DrawString(dataGridView1.Rows[i].Cells["produs_pret"].Value.ToString() , print6B, Brushes.Black, x+105, 120 + height);
  e.Graphics.DrawString(dataGridView1.Rows[i].Cells["produsvaloare"].Value.ToString(), print6B, Brushes.Black, x + 135, 120 + height);
  height += 20;
}
e.Graphics.DrawString("-----------------------------------------", new Font(dataGridView1.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, 10, dataGridView1.Height);
e.Graphics.DrawString("Total:  " + textBox1.Text+" RON", print10B, Brushes.Black,40, dataGridView1.Height + 10);

直到这里:

e.Graphics.DrawString("-----------------------------------------", new Font(dataGridView1.Font.FontFamily, 8, FontStyle.Regular), Brushes.Black, 10, dataGridView1.Height);
e.Graphics.DrawString("Total:  " + textBox1.Text+" RON", print10B, Brushes.Black,40, dataGridView1.Height + 10);

打印机不打印上述部分(最后2个拉绳)。为什么呢?

打印机页面尺寸为比例A4(48 mm x 68 mm)

1 个答案:

答案 0 :(得分:0)

这不是您问题的直接答案,但可能会为您节省大量时间。

Codeproject.com上有代码和组件,允许您打印/打印预览DataGrid。我们在几个应用程序中使用它,它可以节省生命。

http://www.codeproject.com/Articles/36488/DataGridView-Print-Print-Preview-Solution-Part-I