Search code examples
javathermal-printerepson

Epson epos sdk receipt alignment issue


i'm currently using epson ePOS SDK for android. i need to print the receipt that menu name align to the left and its price align to the right in the same line but it doesn't work properly, my temporary solution is add some feed line to make its price align right, is it possible to have both text align left and right in the same line ? (Attachments below and please ignore question mark symbols)

                mPrinter.addTextAlign(Printer.ALIGN_LEFT);
                mPrinter.addFeedLine(0);
                textData.append(menuName);
                mPrinter.addText(textData.toString());
                textData.delete(0, textData.length());
                mPrinter.addFeedLine(0);

                //print price
                mPrinter.addTextAlign(Printer.ALIGN_RIGHT);
                textData.append(price + "Y" + "\n");
                mPrinter.addText(textData.toString());
                textData.delete(0, textData.length());
                mPrinter.addFeedLine(0);

enter image description here


Solution

  • 80mm is like 42 columns per line ...which can be easily padded:

    mPrinter.addText(padLine(menuName, price + "¥", 42) + "\n");
    

    the required String manipulation methods look alike:

    /** utility: pads two strings to columns per line */
    protected String padLine(@Nullable String partOne, @Nullable String partTwo, int columnsPerLine){
        if(partOne == null) {partOne = "";}
        if(partTwo == null) {partTwo = "";}
        String concat;
        if((partOne.length() + partTwo.length()) > columnsPerLine) {
            concat = partOne + " " + partTwo;
        } else {
            int padding = columnsPerLine - (partOne.length() + partTwo.length());
            concat = partOne + repeat(" ", padding) + partTwo;
        }
        return concat;
    }
    
    /** utility: string repeat */
    protected String repeat(String str, int i){
        return new String(new char[i]).replace("\0", str);
    }
    

    one should format the price to currency, before padding it.

    to make it truly "flawless" ... when String concat exceeds length 42, then String partOne should be truncated by the excess length - and be concatenated, once again. exceeding int columnsPerLine will most likely mess up the output.