使用 WinForms 应用程序格式化收据
Format a receipt with WinForms Application
我正在使用餐厅账单打印软件。
我已经开发了收据。但它们看起来并不像收据。
我遵循了 codeProject 的解决方案。这是我创建收据的代码:
//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));
}
我得到的结果如下图所示。我想让它像一张真实的收据。
一栏是数量,另一栏是价格。
(请注意,我遵循的解决方案非常适合他们。)
我目前的输出:
如果您想使用固定字体,例如 Courier
或 Consolas
,您的问题可以通过 padding 左侧部分轻松解决每行一定长度的空格。
下一步是填充右边的部分,这样数字就是right-aligned。
为此你最好写一个小的辅助函数:
string alignedNumber(decimal number, int length)
{
return ("$" + number).PadLeft(length, ' ');
}
所以你写:
g.DrawString("Change: ".PadRight(25, ' ') + alignedNumber(price, 8)...);
..对于所有带列的输出。 (选择你自己的号码!)
如果您选择更改为比例字体,则需要为该位置编写单独的 DrawString
调用并为每个调用设置 x-offset。为了正确对齐,您还必须 测量 使用 Graphics.MeasureString
的数字字符串的宽度。参见
我正在使用餐厅账单打印软件。
我已经开发了收据。但它们看起来并不像收据。
我遵循了 codeProject 的解决方案。这是我创建收据的代码:
//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));
}
我得到的结果如下图所示。我想让它像一张真实的收据。
一栏是数量,另一栏是价格。
(请注意,我遵循的解决方案非常适合他们。)
我目前的输出:
如果您想使用固定字体,例如 Courier
或 Consolas
,您的问题可以通过 padding 左侧部分轻松解决每行一定长度的空格。
下一步是填充右边的部分,这样数字就是right-aligned。
为此你最好写一个小的辅助函数:
string alignedNumber(decimal number, int length)
{
return ("$" + number).PadLeft(length, ' ');
}
所以你写:
g.DrawString("Change: ".PadRight(25, ' ') + alignedNumber(price, 8)...);
..对于所有带列的输出。 (选择你自己的号码!)
如果您选择更改为比例字体,则需要为该位置编写单独的 DrawString
调用并为每个调用设置 x-offset。为了正确对齐,您还必须 测量 使用 Graphics.MeasureString
的数字字符串的宽度。参见