爱普生epos sdk收据对齐问题

时间:2016-10-24 11:49:12

标签: java thermal-printer epson

我目前正在使用epson ePOS SDK for android。 我需要打印菜单名称左对齐的收据,其价格在同一行右侧对齐,但不能正常工作, 我的临时解决方案是添加一些Feed线以使其价格对齐,是否可以在同一行中左右两个文本对齐? (下面的附件,请忽略问号符号)

                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

2 个答案:

答案 0 :(得分:3)

80mm就像每行42列......可以轻松填充:

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

所需的String操作方法看起来很相似:

/** 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);
}

在填充之前,应该将价格格式化为货币。

让它真正“完美”......当String concat超过42时,String partOne应该被多余的长度截断 - 再次连接起来。超过int columnsPerLine将极有可能搞砸输出。

答案 1 :(得分:3)

将RIGHT字符串插入LEFT字符串中的适当位置。这是因为我没有使用EPSON提供的feedLine。取而代之的是,我要在42个字符(视打印机而定)之后手动添加新行(\ n)。

// gap -> Number of spaces between the left and the right string
public void print(Printer epsonPrinter, String left, String right, int gap) throws Epos2Exception {
    int total = 42;

    // This is to give a gap between the 2 columns
    for (int i = 0; i < gap; i++) {
        right = " " + right;
    }

    // This will make sure that the right string is pushed to the right. 
    // If the total sum of both the strings is less than the total length(42),
    // we are prepending extra spaces to the RIGHT string so that it is pushed to the right.
    if ((left.length() + right.length()) < total) {
        int extraSpace = total - left.length() - right.length();
        for (int i = 0; i < extraSpace; i++) {
            left = left + " ";
        }
    }

    int printableSpace = total - right.length(); // printable space left for the LEFT string

    //stringBuilder -> is the modified string which has RIGHT inserted in the LEFT at the appropriate position. And also contains the new line. This is the string which needs to be printed
    StringBuilder stringBuilder = new StringBuilder();
   // Below 2 statements make the first line of the print
    stringBuilder.append(left.substring(0, Math.min(printableSpace, left.length())));
    stringBuilder.append(right);

   // stringBuilder now contains the first line of the print


   // For appropriately printing the remaining lines of the LEFT string
    for (int index = printableSpace; index < left.length(); index = index + (printableSpace)) {
        stringBuilder.append(left.substring(index, Math.min(index + printableSpace, left.length())) + "\n");
    }
    epsonPrinter.addText(stringBuilder.toString());
}
相关问题