I am designing a software application for a Hotel. I want to print the invoice from a thermal printer and I want to print specific columns from the DataGridView
. I have done most of the work but when name of item get larger then the string move out of the print area.
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;
}
}
You have to manually check string length and do text wrapping.
Example:
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);
}