如何在热敏打印机上以特定格式打印发票

时间:2014-12-18 09:01:13

标签: c# graphics printing datagridview printdocument

我正在为酒店设计软件应用程序。我想从热敏打印机打印发票,我想打印DataGridView中的特定列。我完成了大部分工作,但当项目名称变大时,字符串移出打印区域。

void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics graphic = e.Graphics;
    Font font = new Font("Times New Roman", 8, FontStyle.Regular);
    Font font2 = new Font("Times New Roman", 10, FontStyle.Bold);
    float fontHeight = font.GetHeight();
    int startX = 10;
    int startY = 10;
    int offset = 40;
    graphic.DrawString("Welcome to Jaipur Rajwada", new Font("Times New Roman", 10, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY);
    graphic.DrawString("Jaipur-Delhi Highway", new Font("Times New Roman", 9, FontStyle.Italic), new SolidBrush(Color.Black), startX + 15, startY + 18);

    int z = 1;
    foreach (DataGridViewRow dr in dataGridViewSell.Rows)
    {
        string discription = dr.Cells[0].FormattedValue.ToString().PadRight(10);

        string price = string.Format("{0:c}", dr.Cells[2].FormattedValue.ToString());
        string quantity = dr.Cells[3].FormattedValue.ToString();
        string total = string.Format("{0:c}", dr.Cells[4].FormattedValue.ToString());
        string productline = z + " - " + discription + " - " + quantity + " - " + price + " - " + total;
        graphic.DrawString(productline, font, new SolidBrush(Color.Black), startX - 3, startY + offset);
        offset = offset + (int)fontHeight + 5;
        z++;
    }
    offset = offset + 15;
    graphic.DrawString("Total to Pay".PadRight(20) + String.Format("{0:c}", textBoxTotal.Text), font2, new SolidBrush(Color.Black), startX, startY + offset);    
}

private void buttonPrint_Click(object sender, EventArgs e)
{
    try
    {
        printReceipt();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

1 个答案:

答案 0 :(得分:0)

您必须手动检查字符串长度并进行文本换行。

示例:

string columnName = "...";
if(columnName.Length > 60)
{
    graphic.DrawString(columnName_part1, new Font("Times New Roman", 10, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY);
    startY += 50;
    graphic.DrawString(columnName_part2, new Font("Times New Roman", 10, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY);
}