Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print width - Receipt Product and Prize on same line, with name left aligned and price right aligned #147

Open
NewbridgeGareth opened this issue Dec 4, 2021 · 3 comments

Comments

@NewbridgeGareth
Copy link

Hi,
First of all, this is a great library. Well done to all involved.

I would like to know the best way of achieving the following if possible, preferably without using space characters.

I have a product receipt and I want to left align the product name and right align the price on the same line.
Is this possible?
I currently have a lovely looking receipt with the above format, however I have had to right align the price on the line below and it looks a little strange.

I tried doing e.PrintLine(productname + e.RightAlign() + productprice),
but that didnt work and gave me a string output for the e.RightAlign().

Any guidance would be gratefully received.

Thanks again

@igorocampos
Copy link
Collaborator

igorocampos commented Dec 7, 2021

Hello there, you will not be able to achieve that so easily. I believe the printer will override the alignment command when you send it twice in the same row. Maybe there's a printer model that behaves differently, but the ones I've worked with wouldn't allow 2 alignments in the same line.

The solutions I've seen for this were the 2 you have mentioned:
1 - Create your own helper methods based on the printer's max char for one line and create the necessary space padding.
2 - Divide the text in 2 lines, one left aligned and the second right aligned.

@nickcharlton
Copy link
Contributor

nickcharlton commented Jan 26, 2022

I had to solve a similar problem recently, and you won't be able to do it without using spaces (not with a usual ESC/POS printer, anyway). This is the most elegant solution I came up with:

private String EdgeAlign(string left, string right)
{
    var maxWidth = 48;
    var spaces = maxWidth - (left.Length + right.Length);

    return $"{left}{new String(' ', spaces)}{right}";
}

IMG_4885

In case it's useful for anyone else coming across this problem!

@elf-is
Copy link

elf-is commented Nov 11, 2022

Hi another easy way to do this is use C# string padding, you can do something like: e.PrintLine(productname.PadRight(39) + productprice) .
NB: You need to include the length of the string in the total number of characters you include in PadRight

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants