Epson epos sdk收据对齐问题
Epson epos sdk receipt alignment issue
我目前正在为 android 使用 epson ePOS SDK。
我需要在同一行打印菜单名称左对齐且价格右对齐的收据,但它不能正常工作,
我的临时解决方案是添加一些 feed line 使其价格对齐,是否可以让文本在同一行中左右对齐?
(以下附件请无视问号)
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);
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("[=11=]", str);
}
在填充之前,应该将价格格式化为货币。
使其真正成为 "flawless" ... 当 String concat
超过长度 42
时,则 String partOne
应该被多余的长度截断 - 并连接起来,一次再次。超过 int columnsPerLine
很可能会弄乱输出。
在适当的位置插入左字符串中的右字符串。这是因为我没有使用 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());
}
我目前正在为 android 使用 epson ePOS SDK。 我需要在同一行打印菜单名称左对齐且价格右对齐的收据,但它不能正常工作, 我的临时解决方案是添加一些 feed line 使其价格对齐,是否可以让文本在同一行中左右对齐? (以下附件请无视问号)
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);
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("[=11=]", str);
}
在填充之前,应该将价格格式化为货币。
使其真正成为 "flawless" ... 当 String concat
超过长度 42
时,则 String partOne
应该被多余的长度截断 - 并连接起来,一次再次。超过 int columnsPerLine
很可能会弄乱输出。
在适当的位置插入左字符串中的右字符串。这是因为我没有使用 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());
}