How to Print receipt with thermal or dotmatrix printer using C#


The following example shows, how to print sales receipt on a thermal or dotmatrix printer with C# windows form. The output is shown in the following image.

How to Print receipt with thermal or dotmatrix printer using C#
Example
using System.Drawing.Printing;

//Print Button Click Event
private void button1_Click(object sender, EventArgs e)
{
  try
  {
    PrintDocument pd = new PrintDocument();
    PrinterSettings ps = new PrinterSettings();
    pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
    pd.Print();
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message);
  }
}

private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
  System.Drawing.Font headingFont = new System.Drawing.Font("Calibri", 13,System.Drawing.FontStyle.Bold);
  System.Drawing.Font boldFont = new System.Drawing.Font("Calibri", 11, System.Drawing.FontStyle.Bold);
  System.Drawing.Font normalFont = new System.Drawing.Font("Calibri", 11, System.Drawing.FontStyle.Regular);

  float topMargin = ev.MarginBounds.Top;
  float leftMargin = ev.MarginBounds.Left;

  //Create DataTable for Sales Details
  DataTable dt = new DataTable();
  dt.Columns.Add("Product");
  dt.Columns.Add("Price");
  dt.Columns.Add("Qty");
  dt.Columns.Add("Total");

  //Add Sales Details into DataTable
  DataRow dr = dt.NewRow();
  dr[0] = "Apple";
  dr[1] = "2";
  dr[2] = "100.00";
  dr[3] = "200.00";
  dt.Rows.Add(dr);

  dr = dt.NewRow();
  dr[0] = "Orange";
  dr[1] = "10";
  dr[2] = "50.00";
  dr[3] = "500.00";
  dt.Rows.Add(dr);

  dr = dt.NewRow();
  dr[0] = "Strawberry";
  dr[1] = "10";
  dr[2] = "150.00";
  dr[3] = "1500.00";
  dt.Rows.Add(dr);

  string receipt_no = "10002";
  string receipt_date = "29-10-2021";
  string net_total = "2200.00";
  string line = "--------------------------------------------------------------------------------";
  float height = 30;


  //Print Company Name
  ev.Graphics.DrawString("SAMPLE BILL", headingFont, Brushes.Black, 160, height, new StringFormat());
  height += 30;
  //Print Company Address
  ev.Graphics.DrawString("4 th Cross Street, City - Pincode", normalFont, Brushes.Black, 100, height, new StringFormat());
  height += 40;

  //Print Receipt No
  ev.Graphics.DrawString("Receipt No : "+receipt_no, boldFont, Brushes.Black, 10, height, new StringFormat());
  //Print Receipt Date
  ev.Graphics.DrawString("Date : " + receipt_date, boldFont, Brushes.Black, 260, height, new StringFormat());
  height += 40;

  //Print Line
  ev.Graphics.DrawString(line, normalFont, Brushes.Black, 10, height, new StringFormat());
  height += 20;

  //Printe Table Headings
  ev.Graphics.DrawString("Description", normalFont, Brushes.Black, 10, height, new StringFormat());
  ev.Graphics.DrawString("Qty", normalFont, Brushes.Black, 170, height, new StringFormat());
  ev.Graphics.DrawString("Price", normalFont, Brushes.Black, 220, height, new StringFormat());
  ev.Graphics.DrawString("Total", normalFont, Brushes.Black, 320, height, new StringFormat());
  height += 20;

  //Print Line
  ev.Graphics.DrawString(line, normalFont, Brushes.Black, 10, height, new StringFormat());
  height += 20;

  //Printe Table Rows
  for (int i = 0; i < dt.Rows.Count; i++)
  {
    SizeF qtyWidth = ev.Graphics.MeasureString(dt.Rows[i][1].ToString(), normalFont);
    SizeF priceWidth = ev.Graphics.MeasureString(dt.Rows[i][2].ToString(), normalFont);
    SizeF totalWidth = ev.Graphics.MeasureString(dt.Rows[i][3].ToString(), normalFont);

    ev.Graphics.DrawString(dt.Rows[i][0].ToString(), normalFont, Brushes.Black, 10, height, new StringFormat());
    ev.Graphics.DrawString(dt.Rows[i][1].ToString(), normalFont, Brushes.Black, 140+(50 - qtyWidth.Width), height, new StringFormat());
    ev.Graphics.DrawString(dt.Rows[i][2].ToString(), normalFont, Brushes.Black, 220 + (50 - priceWidth.Width), height, new StringFormat());
    ev.Graphics.DrawString(dt.Rows[i][3].ToString(), normalFont, Brushes.Black, 320 + (50 - totalWidth.Width), height, new StringFormat());
    height += 30;
  }
  //Print Line
  ev.Graphics.DrawString(line, normalFont, Brushes.Black, 10, height, new StringFormat());
  height += 20;

  //Print Net Total
  ev.Graphics.DrawString("Total", normalFont, Brushes.Black, 220, height, new StringFormat());

  SizeF netWidth = ev.Graphics.MeasureString(net_total, normalFont);
  ev.Graphics.DrawString(net_total, normalFont, Brushes.Black, 320 + (50 - netWidth.Width), height, new StringFormat());
  height += 20;

  //Print Line
  ev.Graphics.DrawString(line, normalFont, Brushes.Black, 10, height, new StringFormat());
  height += 40;

  ev.Graphics.DrawString("!!! THANK YOU !!!", headingFont, Brushes.Black, 130, height, new StringFormat());

  ev.HasMorePages = false;
}
Download Code