C#: Printing POS Receipt Directly to Printer

Previously I made the VB.NET version here:

https://rani-irsan.blogspot.com/2019/12/vbnet-printing-pos-receipt-directly-to.html

But there're some requests to make the C# version. So here it is direct printing which is often used to print cashier receipts through the POS (Point of Sales) application.

I created 2 classes for supporting the printing process. These classes also allow us to print images or logos and arrange fonts as desired. The classes can be downloaded using the below link:

After downloading those 2 files, copy them into your project/solution as in the image below.


Then open the project/solution using Visual Studio. Go to Solution Explorer then click/activate "show all files" as below. After the files are displayed, select and right-click on it --> Include In Project. Now, we're ready to use it.


On UI Design we'll only add a button into the Form1. Printing will be triggered by Button1_Click()


And here is the behind code:

using System;
using System.Windows.Forms;
using System.Drawing;
using System.Data;

namespace CSharpPrinting
{
    public partial class Form1 : Form
    {
        string StoreName = "SERBA ADA STORE";
        string StoreAddress = "Jl. Kehidupan No. 100";
        Image Img = Image.FromFile(@"c:\logo.jpg");
        string TransNo = "TCN10-20191204-001";
        string TransDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

        //for item sales | untuk item penjualan
        DataTable dtItem;
        int[] arrWidth;
        StringFormat[] arrFormat;

        //'declaring printing format class
        PrintingFormat c = new PrintingFormat();

        //for subtotal & qty total
        double dblSubtotal = 0;
        double dblQty = 0;
        double dblPayment = 50000;


        public void Data_Load()
        {
            dtItem = new DataTable();
            {
                var withBlock = dtItem.Columns;
                withBlock.Add("itemname", Type.GetType("System.String"));
                withBlock.Add("qty", Type.GetType("System.String"));
                withBlock.Add("price", Type.GetType("System.String"));
            }

            DataRow ItemRow;

            ItemRow = dtItem.NewRow();
            ItemRow["itemname"] = "Taro Snack";
            ItemRow["qty"] = "1";
            ItemRow["price"] = "5000";
            dtItem.Rows.Add(ItemRow);

            ItemRow = dtItem.NewRow();
            ItemRow["itemname"] = "Kopi Ice";
            ItemRow["qty"] = "2";
            ItemRow["price"] = "7000";
            dtItem.Rows.Add(ItemRow);

            ItemRow = dtItem.NewRow();
            ItemRow["itemname"] = "Lolipop";
            ItemRow["qty"] = "5";
            ItemRow["price"] = "1000";
            dtItem.Rows.Add(ItemRow);
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            Data_Load();

            SendPrint.NewPrint();

            SendPrint.Print(Img, 100, 50);

            //Setting Font
            SendPrint.SetFont("Courier New", 11, FontStyle.Bold);
            SendPrint.Print(StoreName); //Store Name | Nama Toko

            //Setting Font
            SendPrint.SetFont("Courier New", 8, FontStyle.Regular);
            //Store Address | Alamat Toko
            SendPrint.Print(StoreAddress + ";", new int[]{280}, 0); 

            //spacing
            SendPrint.Print(" ");

            SendPrint.Print(TransNo); // Transaction No | Nomor transaksi
            SendPrint.Print(TransDate); // Trans Date | Tanggal transaksi

            SendPrint.Print(" "); //spacing
            //Setting Font
            SendPrint.SetFont("Courier New", 8, FontStyle.Bold);
            //array for column width | array untuk lebar kolom
            arrWidth = new int[] { 90, 40, 50, 70};
            //array alignment 
            arrFormat = new StringFormat[] { c.MidLeft, c.MidRight,
                                             c.MidRight, c.MidRight }; 

            //column header split by ; | nama kolom dipisah dengan ;
            SendPrint.Print("item;qty;price;subtotal", arrWidth, arrFormat);
            //Setting Font
            SendPrint.SetFont("Courier New", 8, FontStyle.Regular); 
            SendPrint.Print("------------------------------------"); //line

            dblSubtotal = 0;
            dblQty = 0;
            //looping item sales | loop item penjualan
            for (int r = 0; r <= dtItem.Rows.Count - 1; r++)
                {
                  SendPrint.Print(dtItem.Rows[r]["itemname"].ToString() + 
                       ";" + dtItem.Rows[r]["qty"] + ";" +
                       dtItem.Rows[r]["price"] + ";" +
                       (double.Parse(dtItem.Rows[r]["qty"].ToString()) * 
                        double.Parse(dtItem.Rows[r]["price"].ToString())), 
                        arrWidth, arrFormat);
                  dblQty = dblQty + 
                           double.Parse(dtItem.Rows[r]["qty"].ToString());
                  dblSubtotal = dblSubtotal + 
                         (double.Parse(dtItem.Rows[r]["qty"].ToString()) 
                          * double.Parse(dtItem.Rows[r]["price"].ToString()
                          ));
                }

                SendPrint.Print("------------------------------------");
                arrWidth = new int[] { 130, 120 };
                //array for column width | array untuk lebar kolom
                arrFormat = new StringFormat[] { c.MidLeft, c.MidRight };
                //array alignment 

                SendPrint.Print("Total;" + dblSubtotal, arrWidth, arrFormat);
                SendPrint.Print("Payment;" + dblPayment, arrWidth, arrFormat);
                SendPrint.Print("------------------------------------");
                SendPrint.Print("Change;" + 
                                (dblPayment - dblSubtotal).ToString(), 
                                arrWidth, arrFormat);
                SendPrint.Print(" ");
                SendPrint.Print("Item Qty;" + dblQty, arrWidth, arrFormat);

                //Release the job for actual printing
                SendPrint.DoPrint();
        }
    }
}

Let's run and click the button1 to print.


This is the result:


If you want to download the sample project, download it here!



Post a Comment

0 Comments