我想在C#中对齐收据 [英] I want to align receipt in C#

查看:121
本文介绍了我想在C#中对齐收据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,伙计们,请帮助我,我想知道我已经制作了一个餐馆软件,当我打印收据打印收据但对齐是这样的项目显示在行,但我想在收据上对齐价格文本。



我尝试了什么:



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:

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);

推荐答案

那里是DrawString的重载,它允许您指定给定矩形内的文本对齐 - 例如,请参阅文章打印和打印预览DataGrid或DataGridView控件的类 [ ^ ]



优先使用它来尝试用空格填充字符串。
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.


这篇关于我想在C#中对齐收据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆