爱普生epos sdk收据对齐问题

问题描述 投票:2回答:2

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

                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

java thermal-printer epson
2个回答
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将极有可能搞砸输出。


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());
}
© www.soinside.com 2019 - 2024. All rights reserved.