Facebook
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Windows.Forms;

namespace POS_PrintUsingPrintDocument
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private static RootObject ReportData;
        private static int height;
        private static int itemIndex = 0;
        private static int pageNum = 1;
        private static bool printPage = false;
        private static List<PageSizeSettings> PageSizeSettingsList;
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                if (!File.Exists(Environment.ExpandEnvironmentVariables("%allusersprofile%") + "/DefaultPrinter/PageConfiguration.config"))
                {
                    PageSizeSettingsList = new List<PageSizeSettings>();
                    PageSizeSettingsList.Add(new PageSizeSettings());
                    File.WriteAllText(Environment.ExpandEnvironmentVariables("%allusersprofile%") + "/DefaultPrinter/PageConfiguration.config", JsonConvert.SerializeObject(PageSizeSettingsList));
                    FileSecurity(Environment.ExpandEnvironmentVariables("%allusersprofile%") + "/DefaultPrinter/PageConfiguration.config");
                }
                else
                {
                    PageSizeSettingsList = Newtonsoft.Json.JsonConvert.DeserializeObject<List<PageSizeSettings>>(File.ReadAllText(Environment.ExpandEnvironmentVariables("%allusersprofile%") + "/DefaultPrinter/PageConfiguration.config"));
                }
            }
            catch (Exception ex)
            { }
        }
        public void FileSecurity(string FileName)
        {
            FileSecurity fSecurity = File.GetAccessControl(FileName);
            fSecurity.AddAccessRule(new FileSystemAccessRule("Everyone", FileSystemRights.FullControl, AccessControlType.Allow));
            File.SetAccessControl(FileName, fSecurity);
        }

        private void CashReceipt()
        {
            itemIndex = 0;
            pageNum = 1;
            PrintDocument printDocument = new PrintDocument();
            printDocument.PrinterSettings.PrinterName = ReportData.Settings.PrinterName;
            printDocument.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(CreateReceipt);

            printDocument.Print();
        }

        public static void CreateReceipt(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //this prints the reciept
            var pageSetting = PageSizeSettingsList.Where(i => i.PageSize == ReportData.Settings.PageSize).FirstOrDefault();

            Graphics graphic = e.Graphics;

            Font font = new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize); //must use a mono spaced font as the spaces need to line up

            float fontHeight = font.GetHeight();
            height = e.PageSettings.PaperSize.Height - 12;

            int startX = pageSetting.PageSetting.XPoint;
            int startY = pageSetting.PageSetting.YPoint;
            int offset = pageSetting.PageSetting.Offset;
            startY = 10;
            Rectangle rect;
            if (File.Exists(Environment.ExpandEnvironmentVariables("%allusersprofile%") + "/DefaultPrinter/logo.jpg"))
            {
                //rect = new Rectangle(5, startY, pageSetting.PageSetting.LogoWidth, pageSetting.PageSetting.LogoHeight);
                var image = Image.FromFile(Environment.ExpandEnvironmentVariables("%allusersprofile%") + "/DefaultPrinter/logo.jpg");
                graphic.DrawImage(image, pageSetting.PageSetting.LogoXPoint, startY, pageSetting.PageSetting.LogoWidth, pageSetting.PageSetting.LogoHeight);

                offset = offset + pageSetting.PageSetting.LogoHeight;

            }

            rect = new Rectangle(5, startY + offset, pageSetting.PageSetting.RecWidth, 20);
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;

            if (pageNum == 1)
            {
                //Rectangle rect = new Rectangle(5, startY,   POS_PrintUsingPrintDocument.Properties.Settings.Default.ItemWidth, 20);
                StringFormat sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;

                graphic.DrawString(ReportData.Header.HotelName, new Font(pageSetting.PageSetting.HeaderCashFontName, pageSetting.PageSetting.HeaderCashFontSize, FontStyle.Bold), Brushes.Black, rect, sf);
                offset = offset + (int)fontHeight + 5;
                if (ReportData.Header.Address != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.Address, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                if (ReportData.Header.Address1 != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.Address1, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                if (ReportData.Header.Address2 != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.Address2, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                if (ReportData.Header.Address3 != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.Address2, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                if (ReportData.Header.City != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.City, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                if (ReportData.Header.Phone != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.Phone, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                if (ReportData.Header.GSTNo != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.GSTNo, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                if (ReportData.Header.FssaiNo != null)
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Header.FssaiNo, new Font(pageSetting.PageSetting.SubHeaderFontName, pageSetting.PageSetting.SubHeaderFontSize), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                graphic.DrawString("-------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;

                Font fontBoldnBig = new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.BillNoRowFontSize, FontStyle.Bold); //must use a mono spaced font as the spaces need to line up

                if (ReportData.CustomerInfo != null)
                {
                    graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}", "Customer:"), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX, startY + offset);
                    graphic.DrawString(string.Format("{0,-" + "26".ToString() + "}", "Bill No: " + ReportData.Header.BillNo), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX + 180, startY + offset);
                    offset = offset + (int)fontHeight + 3;
                }
                else
                {

                    rect = new Rectangle(startX + pageSetting.PageSetting.QuantityPoint - 10, startY + offset, 140, 20);
                    StringFormat sfright = new StringFormat();
                    sfright.LineAlignment = StringAlignment.Far;
                    sfright.Alignment = StringAlignment.Far;

                    graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}\n", "Bill No: " + ReportData.Header.BillNo), fontBoldnBig, new SolidBrush(Color.Black), startX, startY + offset);
                    graphic.DrawString(string.Format("{0,9:N2}\n", "Date: " + ReportData.Header.DateOfBill), fontBoldnBig, new SolidBrush(Color.Black), rect, sfright);
                    offset = offset + (int)fontHeight + 5;
                }



                if (ReportData.CustomerInfo != null)
                {
                    graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}", ReportData.CustomerInfo.Address1), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX, startY + offset);
                    graphic.DrawString(string.Format("{0,-" + "26".ToString() + "}", "Date: " + ReportData.Header.DateOfBill), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX + 180, startY + offset);
                    offset = offset + (int)fontHeight + 3;
                }



                if (ReportData.CustomerInfo != null)
                {
                    graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}", ReportData.CustomerInfo.Address2), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX, startY + offset);
                    graphic.DrawString(string.Format("{0,-" + "26".ToString() + "}", "Time: " + ReportData.Header.TimeOfBill), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX + 180, startY + offset);
                    offset = offset + (int)fontHeight + 3;
                }
                else
                {
                    if (!string.IsNullOrEmpty(ReportData.Header.TimeOfBill))
                    {
                        graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}", "Time: " + ReportData.Header.TimeOfBill), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX, startY + offset);
                        offset = offset + (int)fontHeight + 3;
                    }
                }

                if (ReportData.CustomerInfo != null)
                {
                    graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}", ReportData.CustomerInfo.Address3), new Font("Segoe UI", (float)8.5), new SolidBrush(Color.Black), startX, startY + offset);
                    offset = offset + (int)fontHeight + 3;
                }

                if (ReportData.CustomerInfo != null)
                {
                    graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}", "Phone: " + ReportData.CustomerInfo.Phone), new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize), new SolidBrush(Color.Black), startX, startY + offset);
                    offset = offset + (int)fontHeight + 3;
                }

                graphic.DrawString("-------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;
                
                rect = new Rectangle(pageSetting.PageSetting.XPoint, startY + offset, pageSetting.PageSetting.RecWidth, 20);
                sf = new StringFormat();
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Near;
                startY = 10;

                Font fontBold = new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize, FontStyle.Bold); //must use a mono spaced font as the spaces need to line up

                graphic.DrawString(string.Format("{0,-" + "26".ToString() + "}", "Menu Item"), fontBold, Brushes.Black, rect, sf);
                sf.Alignment = StringAlignment.Far;
                rect = new Rectangle(startX + pageSetting.PageSetting.QuantityPoint, startY + offset, 35, 20);
                graphic.DrawString(string.Format("{0,6}", "Qty"), fontBold, Brushes.Black, rect, sf);
                rect = new Rectangle(startX + pageSetting.PageSetting.PricePoint, startY + offset, 60, 20);
                graphic.DrawString(string.Format("{0,9}", "Rate"), fontBold, Brushes.Black, rect, sf);
                rect = new Rectangle(startX + pageSetting.PageSetting.AmountPoint, startY + offset, 90, 20);
                graphic.DrawString(string.Format("{0,9}", "Amount"), fontBold, Brushes.Black, rect, sf);

                offset = offset + (int)fontHeight + 5;
                pageNum = pageNum + 1;
            }
            graphic.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;

            while (itemIndex < ReportData.Items.Count && startY + offset <= height)
            {
                Item item = ReportData.Items[itemIndex];
                offset = ItemFormat(item, graphic, font, fontHeight, startX, startY, offset, pageSetting);
                itemIndex = itemIndex + 1;
            }

            if (itemIndex == ReportData.Items.Count && startY + offset <= height)
            {
                graphic.DrawString("-------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;

                Font fontBoldnBig = new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.BillNoRowFontSize, FontStyle.Bold); //must use a mono spaced font as the spaces need to line up

                rect = new Rectangle(startX + pageSetting.PageSetting.TotalPoint, startY + offset, pageSetting.PageSetting.WidthSummary, 20);
                StringFormat sfright = new StringFormat();
                sfright.LineAlignment = StringAlignment.Far;
                sfright.Alignment = StringAlignment.Far;

                graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}\n", "Total"), fontBoldnBig, new SolidBrush(Color.Black), startX, startY + offset);
                graphic.DrawString(string.Format("{0,9:N2}\n", ReportData.Total), fontBoldnBig, new SolidBrush(Color.Black), rect, sfright);
                offset = offset + (int)fontHeight + 5;

                graphic.DrawString("-------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;

                Font fontBold = new Font(pageSetting.PageSetting.CashItemFontName, pageSetting.PageSetting.CashItemFontSize, FontStyle.Bold); //must use a mono spaced font as the spaces need to line up

                var sfSummary = new StringFormat();
                sfSummary.LineAlignment = StringAlignment.Center;
                sfSummary.Alignment = StringAlignment.Near;
                sfSummary.Alignment = StringAlignment.Far;
                foreach (var summary in ReportData.BillSummary)
                {
                    graphic.DrawString(string.Format("{0,-" + "26".ToString() + "}", summary.key), fontBold, new SolidBrush(Color.Black), startX, startY + offset);
                    rect = new Rectangle(startX + pageSetting.PageSetting.AmountPoint, startY + offset, 90, 20);
                    graphic.DrawString(string.Format("{0,9:N2}", summary.value), fontBold, new SolidBrush(Color.Black), rect, sfSummary);
                    offset = offset + (int)fontHeight + 5;
                }

                if (ReportData.BillSummary != null && ReportData.BillSummary.Count > 0)
                {
                    graphic.DrawString("-------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
                    offset = offset + (int)fontHeight + 5;
                }

                rect = new Rectangle(startX + pageSetting.PageSetting.TotalPoint, startY + offset, pageSetting.PageSetting.WidthSummary, 20);

                graphic.DrawString(string.Format("{0,-" + (ReportData.Settings.ItemLength + 6 + 9) + "}\n", "Grand Total"), fontBoldnBig, new SolidBrush(Color.Black), startX, startY + offset);
                graphic.DrawString(string.Format("{0,9:N2}\n", ReportData.GrandTotal), fontBoldnBig, new SolidBrush(Color.Black), rect, sfright);
                offset = offset + (int)fontHeight + 5;

                graphic.DrawString("-------------------------------------------------------------------------", font, new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;

                rect.X = startX;
                rect.Width = pageSetting.PageSetting.AmountPoint + 90;
                rect.Y = startY + offset;

                graphic.DrawString(ReportData.Settings.ThankYouNote, new Font(pageSetting.PageSetting.CashItemFontName, 10), new SolidBrush(Color.Black), rect, format);
                offset = offset + (int)fontHeight + 5;
                if (!string.IsNullOrEmpty(ReportData.Settings.ThankYouNote2))
                {
                    rect.Y = startY + offset;
                    graphic.DrawString(ReportData.Settings.ThankYouNote2, new Font(pageSetting.PageSetting.CashItemFontName, 10), new SolidBrush(Color.Black), rect, format);
                    offset = offset + (int)fontHeight + 5;
                }
                rect.Height = 35;
                rect.Y = startY + offset;
                offset = offset + (int)fontHeight + 5;
                graphic.DrawString(ReportData.Settings.ThankYouNote, new Font("Bauhaus 93", 14), new SolidBrush(Color.Black), rect, format);
                rect.Y = startY + offset;
                offset = offset + (int)fontHeight + 5;
                graphic.DrawString(ReportData.Settings.ThankYouNote, new Font("Bodoni MT Poster Compressed", 14), new SolidBrush(Color.Black), rect, format);
                rect.Y = startY + offset;
                offset = offset + (int)fontHeight + 5;
                graphic.DrawString(ReportData.Settings.ThankYouNote, new Font("Brush Script MT", 14), new SolidBrush(Color.Black), rect, format);
                rect.Y = startY + offset;
                offset = offset + (int)fontHeight + 5;
                rect.Height = 50;
                graphic.DrawString(ReportData.Settings.ThankYouNote, new Font("test", 14), new SolidBrush(Color.Black), rect, format);
                offset = offset + (int)fontHeight + 5;
                printPage = true;
            }

            if (startY + offset < height || printPage == true)
            {
                e.HasMorePages = false;
            }
            else
            {
                e.HasMorePages = true;
            }
        }
        public static int ItemFormat(Item item, Graphics graphic, Font font, float fontHeight, int startX, int startY, int offset, PageSizeSettings pageSetting)
        {
            StringFormat sFormat = new StringFormat(StringFormat.GenericTypographic);
            sFormat.Trimming = StringTrimming.None;
            sFormat.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;
            var outStr = getStrSplit(item.ItemName, pageSetting.PageSetting.ItemLength);
            var outlst = outStr.Split('\n').ToList();

            Rectangle rect = new Rectangle(pageSetting.PageSetting.XPoint, startY + offset, pageSetting.PageSetting.RecWidth, 20);
            StringFormat sf = new StringFormat();
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Near;
            startY = 10;
            graphic.DrawString(string.Format("{0,-" + pageSetting.PageSetting.ItemLength.ToString() + "}", outlst[0]), font, Brushes.Black, rect, sf);
            sf.Alignment = StringAlignment.Far;
            rect = new Rectangle(startX + pageSetting.PageSetting.QuantityPoint, startY + offset, 30, 20);
            graphic.DrawString(string.Format("{0,6}", item.Qty), font, Brushes.Black, rect, sf);
            rect = new Rectangle(startX + pageSetting.PageSetting.PricePoint, startY + offset, 60, 20);
            graphic.DrawString(string.Format("{0,9}", item.Rate), font, Brushes.Black, rect, sf);
            rect = new Rectangle(startX + pageSetting.PageSetting.AmountPoint, startY + offset, 90, 20);
            graphic.DrawString(string.Format("{0,9}", item.ItemAmt), font, Brushes.Black, rect, sf);

            offset = offset + (int)fontHeight + 5;
            sf.Alignment = StringAlignment.Near;
            if (outlst.Count > 1)
            {
                int skip = 1;
                foreach (var i in outlst.Skip(skip))
                {
                    rect = new Rectangle(pageSetting.PageSetting.XPoint, startY + offset, pageSetting.PageSetting.RecWidth, 20);

                    graphic.DrawString(string.Format("{0,-" + "50".ToString() + "}", i), font, Brushes.Black, rect, sf);
                    offset = offset + (int)fontHeight + 5;
                    skip = skip + 1;
                }
            }

            if (!string.IsNullOrEmpty(item.CustRmks))
            {
                var outStrRmk = getStrSplit("Customer Remark: " + item.CustRmks, pageSetting.PageSetting.KitchenItemLength);
                var outlstRmk = outStrRmk.Split('\n').ToList();

                rect = new Rectangle(pageSetting.PageSetting.XPoint, startY + offset, pageSetting.PageSetting.RecWidth, 20);
                graphic.DrawString(string.Format("{0,-" + pageSetting.PageSetting.KitchenItemLength.ToString() + "}", outlstRmk[0]), font, Brushes.Black, rect, sf);
                offset = offset + (int)fontHeight + 5;


                if (outlstRmk.Count > 1)
                {
                    int skip = 1;

                    foreach (var i in outlstRmk.Skip(skip))
                    {
                        rect = new Rectangle(pageSetting.PageSetting.XPoint, startY + offset, pageSetting.PageSetting.RecWidth, 20);

                        graphic.DrawString(string.Format("{0,-" + pageSetting.PageSetting.KitchenItemLength.ToString() + "}", i), font, Brushes.Black, rect, sf);
                        offset = offset + (int)fontHeight + 5;
                        skip = skip + 1;
                    }
                }
            }


            return offset;
        }


        //This function split item name into multiple lines depending upon length parameter
        public static string getStrSplit(string x, int length)
        {
            string result = string.Empty;
            if (x.Length <= length)
            {
                return x;
            }
            else
            {
                var sub = x.Substring(0, length);
                result += sub + "\n" + getStrSplit(x.Replace(sub, ""), length);
            }

            return result;
        }

        private void btnSamplePrint_Click(object sender, EventArgs e)
        {
            string json = File.ReadAllText("Sample.txt");
            ReportData = JsonConvert.DeserializeObject<RootObject>(json);
            CashReceipt();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *