How to make my receipt on POS printer align
object TicketItem have 3 properties:
string Quantity
string Name
string Price
I need to have format like this on 70mm space on paper like below.
----------------------------------
1 x Coca Cola 0,25 4,00
2 x Fantasime 11,00
3 x Some long string item 222,99
name into wrap item
----------------------------------
foreach (var item in ticket.ticketItems)
{
itemString = FormatLineItem((item.quantity.Length == 1 ? item.quantity + " " : 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;
}
public string FormatLineItem(string quantity, string name, string amount)
{
return string.Format("{0}x {1,-10} | {2,5}", quantity, name, amount);
}
My results isn't like in EXAMPLE top please someone solution.
c#
add a comment |
object TicketItem have 3 properties:
string Quantity
string Name
string Price
I need to have format like this on 70mm space on paper like below.
----------------------------------
1 x Coca Cola 0,25 4,00
2 x Fantasime 11,00
3 x Some long string item 222,99
name into wrap item
----------------------------------
foreach (var item in ticket.ticketItems)
{
itemString = FormatLineItem((item.quantity.Length == 1 ? item.quantity + " " : 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;
}
public string FormatLineItem(string quantity, string name, string amount)
{
return string.Format("{0}x {1,-10} | {2,5}", quantity, name, amount);
}
My results isn't like in EXAMPLE top please someone solution.
c#
your code includes an attempt at graphical text output, and one at string alignment for fixed-width raster output. which one is needed?
– dlatikay
Nov 13 '18 at 11:24
@dlatikay that code example coca cola etc.. need solution for all wraped text and align prices to right etc.. can't make it work ...
– MITw1
Nov 13 '18 at 11:27
What is your current output look like?
– Chetan Ranpariya
Nov 13 '18 at 11:34
if you are using GDI+ for printing, it could work when you set a fixed-width (non-proportional) font on the device. Otherwise, you have to draw multiple stretches of text with different alignment settings.
– dlatikay
Nov 13 '18 at 11:35
If you don't use a fixed-pitch font then you need two DrawString() calls, the second one using a different x argument. Do keep in mind that POS printers always like raw output that bypasses the printer driver, you get the fixed-pitch font that's built in the printer as a side-benefit.
– Hans Passant
Nov 13 '18 at 12:37
add a comment |
object TicketItem have 3 properties:
string Quantity
string Name
string Price
I need to have format like this on 70mm space on paper like below.
----------------------------------
1 x Coca Cola 0,25 4,00
2 x Fantasime 11,00
3 x Some long string item 222,99
name into wrap item
----------------------------------
foreach (var item in ticket.ticketItems)
{
itemString = FormatLineItem((item.quantity.Length == 1 ? item.quantity + " " : 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;
}
public string FormatLineItem(string quantity, string name, string amount)
{
return string.Format("{0}x {1,-10} | {2,5}", quantity, name, amount);
}
My results isn't like in EXAMPLE top please someone solution.
c#
object TicketItem have 3 properties:
string Quantity
string Name
string Price
I need to have format like this on 70mm space on paper like below.
----------------------------------
1 x Coca Cola 0,25 4,00
2 x Fantasime 11,00
3 x Some long string item 222,99
name into wrap item
----------------------------------
foreach (var item in ticket.ticketItems)
{
itemString = FormatLineItem((item.quantity.Length == 1 ? item.quantity + " " : 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;
}
public string FormatLineItem(string quantity, string name, string amount)
{
return string.Format("{0}x {1,-10} | {2,5}", quantity, name, amount);
}
My results isn't like in EXAMPLE top please someone solution.
c#
c#
edited Nov 13 '18 at 11:28
Alex.U
1,1882921
1,1882921
asked Nov 13 '18 at 11:18
MITw1MITw1
63
63
your code includes an attempt at graphical text output, and one at string alignment for fixed-width raster output. which one is needed?
– dlatikay
Nov 13 '18 at 11:24
@dlatikay that code example coca cola etc.. need solution for all wraped text and align prices to right etc.. can't make it work ...
– MITw1
Nov 13 '18 at 11:27
What is your current output look like?
– Chetan Ranpariya
Nov 13 '18 at 11:34
if you are using GDI+ for printing, it could work when you set a fixed-width (non-proportional) font on the device. Otherwise, you have to draw multiple stretches of text with different alignment settings.
– dlatikay
Nov 13 '18 at 11:35
If you don't use a fixed-pitch font then you need two DrawString() calls, the second one using a different x argument. Do keep in mind that POS printers always like raw output that bypasses the printer driver, you get the fixed-pitch font that's built in the printer as a side-benefit.
– Hans Passant
Nov 13 '18 at 12:37
add a comment |
your code includes an attempt at graphical text output, and one at string alignment for fixed-width raster output. which one is needed?
– dlatikay
Nov 13 '18 at 11:24
@dlatikay that code example coca cola etc.. need solution for all wraped text and align prices to right etc.. can't make it work ...
– MITw1
Nov 13 '18 at 11:27
What is your current output look like?
– Chetan Ranpariya
Nov 13 '18 at 11:34
if you are using GDI+ for printing, it could work when you set a fixed-width (non-proportional) font on the device. Otherwise, you have to draw multiple stretches of text with different alignment settings.
– dlatikay
Nov 13 '18 at 11:35
If you don't use a fixed-pitch font then you need two DrawString() calls, the second one using a different x argument. Do keep in mind that POS printers always like raw output that bypasses the printer driver, you get the fixed-pitch font that's built in the printer as a side-benefit.
– Hans Passant
Nov 13 '18 at 12:37
your code includes an attempt at graphical text output, and one at string alignment for fixed-width raster output. which one is needed?
– dlatikay
Nov 13 '18 at 11:24
your code includes an attempt at graphical text output, and one at string alignment for fixed-width raster output. which one is needed?
– dlatikay
Nov 13 '18 at 11:24
@dlatikay that code example coca cola etc.. need solution for all wraped text and align prices to right etc.. can't make it work ...
– MITw1
Nov 13 '18 at 11:27
@dlatikay that code example coca cola etc.. need solution for all wraped text and align prices to right etc.. can't make it work ...
– MITw1
Nov 13 '18 at 11:27
What is your current output look like?
– Chetan Ranpariya
Nov 13 '18 at 11:34
What is your current output look like?
– Chetan Ranpariya
Nov 13 '18 at 11:34
if you are using GDI+ for printing, it could work when you set a fixed-width (non-proportional) font on the device. Otherwise, you have to draw multiple stretches of text with different alignment settings.
– dlatikay
Nov 13 '18 at 11:35
if you are using GDI+ for printing, it could work when you set a fixed-width (non-proportional) font on the device. Otherwise, you have to draw multiple stretches of text with different alignment settings.
– dlatikay
Nov 13 '18 at 11:35
If you don't use a fixed-pitch font then you need two DrawString() calls, the second one using a different x argument. Do keep in mind that POS printers always like raw output that bypasses the printer driver, you get the fixed-pitch font that's built in the printer as a side-benefit.
– Hans Passant
Nov 13 '18 at 12:37
If you don't use a fixed-pitch font then you need two DrawString() calls, the second one using a different x argument. Do keep in mind that POS printers always like raw output that bypasses the printer driver, you get the fixed-pitch font that's built in the printer as a side-benefit.
– Hans Passant
Nov 13 '18 at 12:37
add a comment |
1 Answer
1
active
oldest
votes
Add yourself a length checking function?
public string FormatLineItem(int quantity, string name, string amount)
{
return string.Format("{0} x {1}{2}",
EnsureLength(quantity,2,true),
EnsureLength(name,23,true),
EnsureLength(amount,7,false));
}
private string EnsureLength(string input, int requiredLength, bool padRight)
{
if (input.Length > requiredLength) return input.Substring(0, requiredLength);
if (input.Length == requiredLength) return input;
if (padRight)
{
return input.PadRight(requiredLength);
}
else
{
return input.PadLeft(requiredLength);
}
}
My debug trace below... Are you not using a fixed width font? You'd need to check for very long things to make the extra line still...
"1 x Coca Cola 0,25 4,00"
"2 x Fantasime 11,00"
"3 x Some long string item 222,99"
Does'n work at all. Price is not align at right ..sry...
– MITw1
Nov 13 '18 at 12:41
My debug trace added to answer for formatting reasons... Are you not using a fixed width font?
– SazooCat
Nov 13 '18 at 13:49
what happend if 3 string item is longer then that size.. how to wrapp on last space in that lenght and put another part of string bellow the third first part of item. This is fine solution but need word wrap now for middle.
– MITw1
Nov 14 '18 at 19:48
Put in a check function Need2ndLine(name) that checks the length, if it says you need a 2nd line, then do another formatted line of output to print the 2nd half of the info.
– SazooCat
Nov 15 '18 at 13:04
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279859%2fhow-to-make-my-receipt-on-pos-printer-align%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Add yourself a length checking function?
public string FormatLineItem(int quantity, string name, string amount)
{
return string.Format("{0} x {1}{2}",
EnsureLength(quantity,2,true),
EnsureLength(name,23,true),
EnsureLength(amount,7,false));
}
private string EnsureLength(string input, int requiredLength, bool padRight)
{
if (input.Length > requiredLength) return input.Substring(0, requiredLength);
if (input.Length == requiredLength) return input;
if (padRight)
{
return input.PadRight(requiredLength);
}
else
{
return input.PadLeft(requiredLength);
}
}
My debug trace below... Are you not using a fixed width font? You'd need to check for very long things to make the extra line still...
"1 x Coca Cola 0,25 4,00"
"2 x Fantasime 11,00"
"3 x Some long string item 222,99"
Does'n work at all. Price is not align at right ..sry...
– MITw1
Nov 13 '18 at 12:41
My debug trace added to answer for formatting reasons... Are you not using a fixed width font?
– SazooCat
Nov 13 '18 at 13:49
what happend if 3 string item is longer then that size.. how to wrapp on last space in that lenght and put another part of string bellow the third first part of item. This is fine solution but need word wrap now for middle.
– MITw1
Nov 14 '18 at 19:48
Put in a check function Need2ndLine(name) that checks the length, if it says you need a 2nd line, then do another formatted line of output to print the 2nd half of the info.
– SazooCat
Nov 15 '18 at 13:04
add a comment |
Add yourself a length checking function?
public string FormatLineItem(int quantity, string name, string amount)
{
return string.Format("{0} x {1}{2}",
EnsureLength(quantity,2,true),
EnsureLength(name,23,true),
EnsureLength(amount,7,false));
}
private string EnsureLength(string input, int requiredLength, bool padRight)
{
if (input.Length > requiredLength) return input.Substring(0, requiredLength);
if (input.Length == requiredLength) return input;
if (padRight)
{
return input.PadRight(requiredLength);
}
else
{
return input.PadLeft(requiredLength);
}
}
My debug trace below... Are you not using a fixed width font? You'd need to check for very long things to make the extra line still...
"1 x Coca Cola 0,25 4,00"
"2 x Fantasime 11,00"
"3 x Some long string item 222,99"
Does'n work at all. Price is not align at right ..sry...
– MITw1
Nov 13 '18 at 12:41
My debug trace added to answer for formatting reasons... Are you not using a fixed width font?
– SazooCat
Nov 13 '18 at 13:49
what happend if 3 string item is longer then that size.. how to wrapp on last space in that lenght and put another part of string bellow the third first part of item. This is fine solution but need word wrap now for middle.
– MITw1
Nov 14 '18 at 19:48
Put in a check function Need2ndLine(name) that checks the length, if it says you need a 2nd line, then do another formatted line of output to print the 2nd half of the info.
– SazooCat
Nov 15 '18 at 13:04
add a comment |
Add yourself a length checking function?
public string FormatLineItem(int quantity, string name, string amount)
{
return string.Format("{0} x {1}{2}",
EnsureLength(quantity,2,true),
EnsureLength(name,23,true),
EnsureLength(amount,7,false));
}
private string EnsureLength(string input, int requiredLength, bool padRight)
{
if (input.Length > requiredLength) return input.Substring(0, requiredLength);
if (input.Length == requiredLength) return input;
if (padRight)
{
return input.PadRight(requiredLength);
}
else
{
return input.PadLeft(requiredLength);
}
}
My debug trace below... Are you not using a fixed width font? You'd need to check for very long things to make the extra line still...
"1 x Coca Cola 0,25 4,00"
"2 x Fantasime 11,00"
"3 x Some long string item 222,99"
Add yourself a length checking function?
public string FormatLineItem(int quantity, string name, string amount)
{
return string.Format("{0} x {1}{2}",
EnsureLength(quantity,2,true),
EnsureLength(name,23,true),
EnsureLength(amount,7,false));
}
private string EnsureLength(string input, int requiredLength, bool padRight)
{
if (input.Length > requiredLength) return input.Substring(0, requiredLength);
if (input.Length == requiredLength) return input;
if (padRight)
{
return input.PadRight(requiredLength);
}
else
{
return input.PadLeft(requiredLength);
}
}
My debug trace below... Are you not using a fixed width font? You'd need to check for very long things to make the extra line still...
"1 x Coca Cola 0,25 4,00"
"2 x Fantasime 11,00"
"3 x Some long string item 222,99"
edited Nov 13 '18 at 13:51
answered Nov 13 '18 at 11:43
SazooCatSazooCat
1025
1025
Does'n work at all. Price is not align at right ..sry...
– MITw1
Nov 13 '18 at 12:41
My debug trace added to answer for formatting reasons... Are you not using a fixed width font?
– SazooCat
Nov 13 '18 at 13:49
what happend if 3 string item is longer then that size.. how to wrapp on last space in that lenght and put another part of string bellow the third first part of item. This is fine solution but need word wrap now for middle.
– MITw1
Nov 14 '18 at 19:48
Put in a check function Need2ndLine(name) that checks the length, if it says you need a 2nd line, then do another formatted line of output to print the 2nd half of the info.
– SazooCat
Nov 15 '18 at 13:04
add a comment |
Does'n work at all. Price is not align at right ..sry...
– MITw1
Nov 13 '18 at 12:41
My debug trace added to answer for formatting reasons... Are you not using a fixed width font?
– SazooCat
Nov 13 '18 at 13:49
what happend if 3 string item is longer then that size.. how to wrapp on last space in that lenght and put another part of string bellow the third first part of item. This is fine solution but need word wrap now for middle.
– MITw1
Nov 14 '18 at 19:48
Put in a check function Need2ndLine(name) that checks the length, if it says you need a 2nd line, then do another formatted line of output to print the 2nd half of the info.
– SazooCat
Nov 15 '18 at 13:04
Does'n work at all. Price is not align at right ..sry...
– MITw1
Nov 13 '18 at 12:41
Does'n work at all. Price is not align at right ..sry...
– MITw1
Nov 13 '18 at 12:41
My debug trace added to answer for formatting reasons... Are you not using a fixed width font?
– SazooCat
Nov 13 '18 at 13:49
My debug trace added to answer for formatting reasons... Are you not using a fixed width font?
– SazooCat
Nov 13 '18 at 13:49
what happend if 3 string item is longer then that size.. how to wrapp on last space in that lenght and put another part of string bellow the third first part of item. This is fine solution but need word wrap now for middle.
– MITw1
Nov 14 '18 at 19:48
what happend if 3 string item is longer then that size.. how to wrapp on last space in that lenght and put another part of string bellow the third first part of item. This is fine solution but need word wrap now for middle.
– MITw1
Nov 14 '18 at 19:48
Put in a check function Need2ndLine(name) that checks the length, if it says you need a 2nd line, then do another formatted line of output to print the 2nd half of the info.
– SazooCat
Nov 15 '18 at 13:04
Put in a check function Need2ndLine(name) that checks the length, if it says you need a 2nd line, then do another formatted line of output to print the 2nd half of the info.
– SazooCat
Nov 15 '18 at 13:04
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53279859%2fhow-to-make-my-receipt-on-pos-printer-align%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
your code includes an attempt at graphical text output, and one at string alignment for fixed-width raster output. which one is needed?
– dlatikay
Nov 13 '18 at 11:24
@dlatikay that code example coca cola etc.. need solution for all wraped text and align prices to right etc.. can't make it work ...
– MITw1
Nov 13 '18 at 11:27
What is your current output look like?
– Chetan Ranpariya
Nov 13 '18 at 11:34
if you are using GDI+ for printing, it could work when you set a fixed-width (non-proportional) font on the device. Otherwise, you have to draw multiple stretches of text with different alignment settings.
– dlatikay
Nov 13 '18 at 11:35
If you don't use a fixed-pitch font then you need two DrawString() calls, the second one using a different x argument. Do keep in mind that POS printers always like raw output that bypasses the printer driver, you get the fixed-pitch font that's built in the printer as a side-benefit.
– Hans Passant
Nov 13 '18 at 12:37