Print on thermal printer

Print Receipt With Thermal Printer

In C Sharp, Programming, Programming Projects by Baqir Ali

While creating a POS (Point of sale) application, it is very important to know how to print the customer receipt on a thermal printer with the windows form. There are many ways to do that but we are going to learn the easiest way to do that.

All the data that are printed on receipt will be brought from an SQL Server database called POS with a table called Deployment.

Windows Form and Thermal Receipt Printer
Windows Form and Thermal Receipt Printer

We are going to extract data from a specific date and print it on a thermal printer.

Create a project in Visual Studio called Receipt. Create windows form with a button on it. Label the button ‘Print’. Then call the Important library

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

Now create the function that should extract data from the database called POS.

  1. Set a connection string to connect with a database called POS
  2. Pass a query to extract data from the database for specific data.
  3. Fill it inside a data table called dt.
  4. and return the data table.
public DataTable getDaily(DateTime date)
        {
            try
            {
                SqlConnection connection = new SqlConnection(@"data source=(local)\SQLEXPRESS; database=POS; Integrated Security=True");
                connection.Open();
                using (SqlDataAdapter ds1 = new SqlDataAdapter("select toLocation,Name,sum(quantity) as quantity from deployment where date='" + this.date + "' group by toLocation,Name order by toLocation", connection.ToString()))
                {
                    DataSet ds = new DataSet();
                    ds1.Fill(ds);
                    DataTable dt = new DataTable();
                    dt = ds.Tables[0];
                    connection.Close();
                    return dt;
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Could not get Data");
            }
        }

On the button’s click event call, the following code will run.

  1. The returned data table will be stored inside a global/class data table called wStock.
  2. Then we create a PrintDialog object to set print properties such as paper size and printer name
  3. Then dailyDep function will be called to generate print page.
private void button1_Click(object sender, EventArgs e)
{
            getDaily(DateTime.Now);
            wStock = this.getDaily(DateTime.Now);

            PrintDialog pd = new PrintDialog();
            pdoc = new PrintDocument();
            PrinterSettings ps = new PrinterSettings();
            Font font = new Font("calibri", 15);
            PaperSize psize = new PaperSize("Custom", 100, 30000);
            pd.Document = pdoc;
            pd.Document.DefaultPageSettings.PaperSize = psize;
            pdoc.DefaultPageSettings.PaperSize.Height = 30000;
            pdoc.DefaultPageSettings.PaperSize.Width = 520;
            pdoc.PrinterSettings.PrinterName = "POS";
            pdoc.PrintPage += new PrintPageEventHandler(dailyDep);
            pdoc.Print();
            pdoc.PrintPage -= new PrintPageEventHandler(dailyDep);
}

In the above code “POS” is named for the thermal printer installed on your PC.

From the click event function, another function will be called, which prints the data on the printer. the data is grouped kitchen-wise. Each kitchen data will be printed separately.

PrintDocument pdoc = null;
DataTable wStock;

void dailyDep(object sender, PrintPageEventArgs e)
        {
            try
            {
                //-----------
                Graphics graphics = e.Graphics;
                Font font = new Font("calibri", 10);
                float fontHeight = font.GetHeight();
                String underLine = "-------------------------------------------------------------";
                int startX = 10;
                int startY = 20;
                int Offset = 10;
                Offset += 0;
                Offset = Offset + 15;
                graphics.DrawString(underLine, new Font("calibri", 10), new SolidBrush(Color.Black), 0, startY + Offset);
                Offset = Offset + 15;
                graphics.DrawString("Daily Deployment For Date " +DateTime.Now.ToString("d, MMMM, yyyy"), new Font("Calibri", 10), new SolidBrush(Color.Black), startX + 15, startY + Offset);
                Offset = Offset + 15;
                graphics.DrawString(underLine, new Font("calibri", 10), new SolidBrush(Color.Black), 0, startY + Offset);
                double tAmount = 0;
                double gTotal = 0;
                string xParent = "*";
                for (int i = 0; i < wStock.Rows.Count; i++)
                {
                    string parent = wStock.Rows[i]["toLocation"].ToString();
                    if (parent != xParent)
                    {
                        tAmount = 0;
                        Offset = Offset + 15;
                        graphics.DrawString(parent.ToUpper() + " SECTION", new Font("Calibri", 10, FontStyle.Bold), new SolidBrush(Color.Black), startX, startY + Offset);
                        Offset = Offset + 15;
                        graphics.DrawString(underLine, new Font("calibri", 10), new SolidBrush(Color.Black), 0, startY + Offset);
                        Offset = Offset + 10;
                        graphics.DrawString("Item                                                   Quantity", new Font("Calibri", 10), new SolidBrush(Color.Black), startX, startY + Offset);
                        Offset = Offset + 10;
                        graphics.DrawString(underLine, new Font("calibri", 10), new SolidBrush(Color.Black), 0, startY + Offset);
                        Offset = Offset + 15;
                        xParent = parent;
                    }
                    string item = (i + 1) + " - " + wStock.Rows[i]["Name"].ToString();
                    string quantity = wStock.Rows[i]["quantity"].ToString();
                    graphics.DrawString(item, new Font("Calibri", 8), new SolidBrush(Color.Black), startX, startY + Offset);
                    graphics.DrawString(quantity, new Font("Calibri", 8), new SolidBrush(Color.Black), startX + 180, startY + Offset);
                    Offset = Offset + 15;
                    string nextParent;
                    if (i < wStock.Rows.Count - 1)
                        nextParent = wStock.Rows[i + 1]["toLocation"].ToString();
                    else
                        nextParent = "*";
                }
                graphics.DrawString(underLine, new Font("calibri", 10), new SolidBrush(Color.Black), 0, startY + Offset);
                Offset = Offset + 15;
                graphics.DrawString("Print Time :  " + DateTime.Now.ToString("d, MMMM, yyyy. hh:mm - tt "), new Font("Calibri", 10), new SolidBrush(Color.Black), startX, startY + Offset);
                Offset = Offset + 15;
                
                graphics.DrawString("By : www.codemodes.com", new Font("Calibri", 10), new SolidBrush(Color.Black), startX, startY + Offset);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }

The output will be

Thermal Receipt with Windows Form

Download The Project

Support us by sharing this post