Search code examples
delphidelphi-7

Delphi POS printing receipt text alignment


How can I print to POS printer if the output format should be like this? paper size 3 inches

line 1 = ITEM DESCRIPTION 
line 2 = QTY UNIT x UNIT PRICE_ _ _ _ _ _ _ TOTAL PRICE
Total PRICE is right align

sample format

BOND PAPER
1 REAM x 100.00 --------------- 100.00
BOND PAPER 2
2 REAM X 100.00 --------------- 200.00
BOND PAPER 3
1 REAM X 1,354.00 ----------- 1,354.00

Solution

  • POS printers typically use fixed width fonts, so right aligning the value of TotalPrice is simply a matter of calculating the amount of Padding to insert into the line after the ItemDescription.

    In your example, you are using a 38 character line, so if the length of ItemDescription is 15 characters long, and the length of the TotalPrice is 6 characters long, then Padding needs to be
    38 - (ItemDescription + TotalPrice) = 38 - (15 + 6) = 17 characters long. But since you seem to add a space immediately after ItemDescription and before TotalPrice your Padding needs to subtract these 2 additional characters... so, in that case Padding needs to be 15 characters long.

    Applying this to your last line:

    Length(ItemDescription) = 17
    Length(TotalPrice) = 8
    Padding = 38 - (17 + 8 + 2) = 11
    

    So the final line that you will send to your fixed width font POS will be:

    PrintLine = Concat(ItemDescription,' ',StringOfChar('-',Padding),' ', TotalPrice)
    

    This should always right align TotalPrice for the given fixed width character paper size (change the 38 to whatever the number of characters your POS printer is rated at), and all long as the total length of ItemDescription, TotalPrice and your single character spaces does not exceed the total character width of the printer (you should probably check this before calculating Padding).