使用WinForms应用程序格式化收据 [英] Format a receipt with WinForms Application

查看:85
本文介绍了使用WinForms应用程序格式化收据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用饭店帐单打印软件.

I'm working with a restaurant bill printing software.

我开了一张收据.但它们实际上看起来并不像收据.

I've developed a receipt. but they don't look actually like a receipt.

我遵循了codeProject的解决方案.这是我制作收据的代码:

I've followed a solution from codeProject. Here is my code of building a receipt:

//Get the Graphics object
        Graphics g = printTextEvArgs.Graphics;

        //Create a font Arial with size 16
        Font font = new Font("Arial", 10);
        float fontHeight = font.GetHeight();
        string underLine = "------------------------------------------";

        int startX = 10;
        int startY = 10;
        int offset = 40;

        //Create a solid brush with black color
        SolidBrush brush = new SolidBrush(Color.Black);
        if (RecieptType == "ktcprinter")
        {
        }
        else if (RecieptType == "billprinter")
        {
            g.DrawString(restaurantInfo.name, new Font("Courier New", 16), new SolidBrush(Color.Black), startX, startY);
            offset = offset + (int)fontHeight + 20;

            var wc = new WebClient();
            Image imgFromUrl = Image.FromStream(wc.OpenRead(b.restaurant_info.logo));
            g.DrawImage(imgFromUrl, 60, 40, 150, 100);

            offset = offset + (int)fontHeight + 50;

            g.DrawString("Address: " + restaurantInfo.address, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            g.DrawString("Phone: " + restaurantInfo.phone, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            g.DrawString("Vat Reg. No.: " + restaurantInfo.vat_reg_no, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            g.DrawString("Served By: " + employeeInfo.served_by, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + 13;
            g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + 13;

            foreach (var item in b.items)
            {
                string menuTitle = item.menu_title + item.quantity + item.price;
                g.DrawString(menuTitle, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;
            }

            //Get UnderLine
            offset = offset - 8;
            g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + 15;

            g.DrawString("Sub Total: " + calculation.sub_total, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            g.DrawString("Discount: " + calculation.discount, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;                
            g.DrawString("Vat: " + calculation.vat, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            g.DrawString("Service Charge: " + calculation.service_charge, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            g.DrawString("Delivery Charge: " + calculation.delivery_charge, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);

            //Get UnderLine
            offset = offset + 12;
            g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + 12;

            g.DrawString("Total: " + calculation.total.PadRight(30), new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;

            //Get UnderLine
            offset = offset - 11;
            g.DrawString(underLine, new Font("Courier New", 12), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + 13;


            foreach (var item in b.payments)
            {
                string paymentMethod = item.method + item.amount;
                g.DrawString(paymentMethod, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
                offset = offset + (int)fontHeight + 5;
            }

            g.DrawString("Change: " + calculation.change, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            g.DrawString("Pay Tip: " + calculation.pay_tip, new Font("Courier New", 10), new SolidBrush(Color.Black), startX, startY + offset);
            offset = offset + (int)fontHeight + 5;
            offset = offset + 20;

            //offset = offset + 20;
            //g.DrawString("Total To Pay".PadRight(30) + string.Format("{0:c}"), variable, font, new SolidBrush(Color.Black), startX, startY + offset);
        }

        //Draw "Hello Printer!";
        //new Rectangle(margin X, margin Y, Width, Height)
        //g.DrawString(printString.ToString(),
        //font, brush, new Rectangle(5, 0, 350, 500));
    }

我得到的就像下面的图片.我想把它做成真实的收据.

What I got is like the below image. I want to make it like a real receipt.

数量应在一栏中,价格应在另一栏中.

The quantities shall be in a column and the price in another column.

(请注意,我遵循的解决方案非常适合他们.)

(Note that, The solution I followed worked perfectly for them.)

此刻我的输出:

推荐答案

如果要使用固定字体(如CourierConsolas),则可以通过填充轻松解决问题>每行的左侧部分应留有一定长度,并带有空格.

If you want to go with a fixed font, like Courier or Consolas, your problem can be easily solved by padding the left portion of each line to a certain length with spaces.

下一步是填充右侧部分,以使数字右对齐.

The next step is to pad the right part so that the numbers are right-aligned.

为此,您最好编写一个小的辅助函数:

For this you best write a small helper function:

string alignedNumber(decimal number, int length)
{
    return ("$" + number).PadLeft(length, ' ');
}

所以你写:

 g.DrawString("Change: ".PadRight(25, ' ') + alignedNumber(price, 8)...);

..对于所有带列的输出. (选择您自己的电话号码!)

..for all your output with columns. (Pick your own numbers!)

如果选择更改为比例字体,则需要为该位置编写单独的DrawString调用并为每个位置设置x偏移.为了正确对齐,您还必须使用Graphics.MeasureString 测量数字字符串的宽度.有关示例,请参见此处

If you choose to change to a proportional font you would need to write separate DrawString calls for the position and set the x-offset for each. For the right alignment you would also have to measure the width the number strings will have using Graphics.MeasureString. See here for an example

这篇关于使用WinForms应用程序格式化收据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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