Click here to Skip to main content
15,907,687 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;
                }
            }
Posted
Updated 8-Nov-18 5:40am
v2
Comments
Alek Massey 8-Nov-18 11:22am    
And what does your current output look like?

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.
 
Share this answer
 
v2
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:

C#
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;
}
 
Share this answer
 
Comments
Member 11381811 9-Nov-18 3:34am    
line width i need to somehow calc how much i have paper size - left and right margin?
Member 11381811 9-Nov-18 4:00am    
foreach (var item in ticket.ticketItems)
{
//e.Graphics.DrawString(Environment.NewLine, printFont, Brushes.Black, x, y);
//y += lineOffset;
itemString = FormatLineItem(1, 60, item.quantity, item.name, item.price);
e.Graphics.DrawString(itemString, printFont, Brushes.Black, x, y);
y += lineOffset;
//e.Graphics.DrawString(Environment.NewLine, printFont, Brushes.Black, x, y);
//y += lineOffset;
}
Need to know how to calclulate line width and need to txt wrap for item name if its so long
i try this but result is lame..

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