当我想在pos打印机上打印时,如何对齐和自动换行? [英] How do I align and word wrap when I want to print on pos printer?

查看:324
本文介绍了当我想在pos打印机上打印时,如何对齐和自动换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Print Nippon打印机上使用PrintDocument类打印一些收据。

所以我需要一些culumns解决方案(收货时的项目).. 重复解决方案

当我有长项目名称需要包装时,如示例项目3

I need to print with PrintDocument class some Receipt on POS Nippon printer.
So i need some solution for culumns (items on receipt)..repeat SOLUTION
And when i have long item name need word wrapping like in example item 3

Qty Name                    Amount
==================================
1 x Coca Cola 0.25            2,00
2 x Filet Steak              11,00
  1 x mushrooms (1,00)
3 x poaspodpaoisdoiapodsipoaipoido
adslkjasldj                  12,00
==================================
                  Total:     25,00



这是我需要打印的例子。 />


我尝试过:



...




this is example what i need to be print.

What I have tried:

...

string itemString = string.Empty;
            string modifierString = string.Empty;
            foreach (var item in ticket.ticketItems)
            {
                itemString = (item.quantity + "x" + item.name).PadLeft(0) + item.price.PadLeft(40); ;
                e.Graphics.DrawString(itemString, printFont, headerBrush, x, y);
                e.Graphics.DrawString(Environment.NewLine, printFont, Brushes.Black, x, y);
                y += lineOffset;

                foreach (var mod in item.modifiers)
                {
                    modifierString = (mod.quantity + "x" + mod.name).PadLeft(0) + mod.price.PadLeft(20);
                    e.Graphics.DrawString(modifierString, printFont, headerBrush, x, y);
                    e.Graphics.DrawString(Environment.NewLine, printFont, Brushes.Black, x, y);
                    y += lineOffset;
                }
            }

推荐答案

这里的关键不是打印机,而是你的纸张尺寸正在使用。您需要获取页面的尺寸,然后使用 Graphics.MeasureString方法(System.Drawing)| Microsoft Docs [ ^ ]调整输出以适应。
The key here is not the printer, but the dimensions of the paper that you are using. You need to get the dimensions of the page and then use methods such as Graphics.MeasureString Method (System.Drawing) | Microsoft Docs[^] to adjust your output to fit.


0)您需要使用非比例字体。 />


1)您需要编写将输出字符串格式化为所需宽度的代码。



2)我会认真考虑将名称截断为最大大小,以便您不会为单个行项目获取多行。这将大大简化代码。



3)以下是一些粗略且未经测试的代码供您开始:



0) You need to use a non-proportional font.

1) You nee to write code that will format your outputted strings to the desired width.

2) I would think seriously about truncating names to a maximum size so that you don't get multiple lines for a single line item. This would greatly simplify the code.

3) Here's some rough and untested code for you to start with:

public string FormatLineItem(int lineType, int linewidth, int quantity, string name, double amount)
{
    int qtyWidth = 3;
    int amtWidth = 5;
    int nameWidth = linewidth - (qtyWidth + amtWidth);
    string result = string.Empty;

    switch (lineType)
    {
        case 1 : // data
            {
                result = string.Format("{0:#0}x {1} {2:##.#0}", quantity, name.PadRight(nameWidth), amount);
            }
            break;
        case 2 : // header
            {
                result = string.Format("QTY {0} AMOUNT",  name.PadRight(nameWidth));
            }
            break;
        case 3 : // footer
            {
                result = string.Format("{0:##.#0"}, amount).PadLeft(linewidth-amtWidth));
            }
            break;
    }
    return result;
}


这篇关于当我想在pos打印机上打印时,如何对齐和自动换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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