【问题标题】:Epson epos sdk receipt alignment issueEpson epos sdk 收据对齐问题
【发布时间】:2026-01-05 05:00:02
【问题描述】:

我目前正在使用 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);

【问题讨论】:

    标签: java thermal-printer epson


    【解决方案1】:

    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 很可能会弄乱输出。

    【讨论】:

      【解决方案2】:

      在 LEFT 字符串的适当位置插入 RIGHT 字符串。这是因为我没有使用 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());
      }
      

      【讨论】:

      • 你能补充一些解释吗?单独的纯代码块通常并不能提供所有信息