Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.31/5 (3 votes)
See more:
Hello Guys,Please help me i want to know i have made a restaurant software when i print receipt it's printing receipt but alignment is so out items shows in line but i want align price text on receipt.

What I have tried:

C#
private void printreceipt()
        {
            PrintDialog printdialog = new PrintDialog();
            PrintDocument printdocument = new PrintDocument();
            PaperSize bp = new PaperSize("Custom", 100, 200);
            printdialog.Document = printdocument;
            printdocument.PrintPage += new PrintPageEventHandler(printdocument_PrintPage);
            printdocument.DefaultPageSettings.PaperSize = bp;
            printdocument.DefaultPageSettings.PaperSize.Height =;
            printdocument.DefaultPageSettings.PaperSize.Width = 320;
            printdocument.PrinterSettings.PrinterName = setaluefortextprinter;
            //DialogResult result = printdialog23.ShowDialog();
            //if (result == DialogResult.OK)
            //{
            printdocument.Print();
            //}
        }
        void printdocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            Graphics graphics = e.Graphics;
            Font f = new Font("Arial", 8);
            float fontHeight = f.GetHeight();
            int starto = 30;
            int startp = 6;
            int startX = 10;
            int startY = 10;
            int offset = 40;
            int offset1 = 260;
            int offset2 = 280;
            int line = 1;
            int line1 = 5;
            int line2 = 6;
            int line3 = 24;
            int line4 = 124;
            int line5 = 32;
            int line6 = 20;
            int line7 = 3;
            int line8 = 6;
            int line9 = 32;
            int offset3 = 160;
            int offset4 = 170;
            int offset5 = 180;

            //graphics.DrawString(label131.Text, new Font("Courier New", 6), new SolidBrush(Color.Black), line, line1);
            graphics.DrawString(setaluefortext00001name, new Font("Courier New", 15), new SolidBrush(Color.Black), starto, startp);
            graphics.DrawString("*************************************************************************************", new Font("Courier New", 6), new SolidBrush(Color.Black), line2, line3);
            graphics.DrawString(label4.Text, new Font("Courier New", 6), new SolidBrush(Color.Black), line8, line9);
            //graphics.DrawString("Receipt#:" + label2.Text, new Font("Courier New", 6), new SolidBrush(Color.Red), line4, line5);
            foreach (tblProduct product in products)
            {
                string productDescription = product.productName;
                string productTotal = " Rs " + string.Format("{0:}", product.productPrice);
                string prodcutLine = productDescription + productTotal;

                graphics.DrawString(prodcutLine, f, new SolidBrush(Color.Black), startX, startY + offset);

                offset = offset + (int)fontHeight + 5;
            }
            offset = offset + 20;
            tblTransaction transaction = new tblTransaction();
            transaction.transactionDate = DateTime.Now;
            graphics.DrawString("Total To Pay".PadRight(15) + "Rs " + string.Format("{0:}", total), f, new SolidBrush(Color.Black), startX, startY + offset);
            graphics.DrawString("Thank You For Coming,".PadRight(15) + string.Format(""), f, new SolidBrush(Color.Black), startX, startY + offset3);
            graphics.DrawString("Please Come Back Soon".PadRight(15) + "" + string.Format(""), f, new SolidBrush(Color.Black), startX, startY + offset4);
            //graphics.DrawString("03147450301".PadRight(15) + "" + string.Format(""), f, new SolidBrush(Color.Black), startX, startY + offset5);
Posted
Updated 5-Sep-16 0:49am
v2

1 solution

There is an overload to DrawString which allows you to specify the text alignment within a given rectangle - for example see the article A class to print and print preview a DataGrid or DataGridView control[^]

Use that in preference to trying to pad the string with spaces.
 
Share this answer
 
Comments
Member 9983063 4-Sep-16 17:27pm    
Hey bro thanks for your reply but this articles not help me please you tell me which thing am using wrong in my code
Duncan Edwards Jones 4-Sep-16 17:47pm    
You are using PadRight to align text.
Don't use that.

Use StringAlignment.
Member 9983063 4-Sep-16 17:49pm    
sir am new on programming can you please tell me what is string alignment
Duncan Edwards Jones 4-Sep-16 17:53pm    
See this Microsoft documentation on the function DrawString that you ar eusing to print:- https://msdn.microsoft.com/en-us/library/21kdfbzs(v=vs.110).aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900