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();
        }
    }
}
0 Comments