How to print the text right aligned in a Bluetooth POS Printer?


How to print the text right aligned in a Bluetooth POS Printer?



I have been trying to do a POS app in android where I am printing the receipt on a Bluetooth printer connected to the app. I am able to print 'large', 'small' and QR code with the HOINSDK provided with the device and also I am able to align then 'left' and 'centre' but unable to align it to the 'right'. Any help is appreciated.



Below are the codes for respective things:


//For Large Text

cmd[0] = 27;
cmd[1] = 97;
cmd[2] &= 0xEF;
mService.write(cmd);
mService.sendMessage("" + large_txt.getText().toString(), "GBK");

//For Small Text
cmd[0] = 0x1b;
cmd[1] = 0x21;
cmd[2] &= 0xEF;
mService.write(cmd);
mService.sendMessage("" + small_txt.getText().toString(), "GBK");

//For Center Alignment small text
cmd[0] = 27;
cmd[1] = 97;
cmd[2] |= 1;
mService.write(cmd);
cmd[0] = 27;
cmd[1] = 97;
cmd[2] = 1;
mService.write(cmd);
mService.sendMessage("" + center_txt.getText().toString(), "GBK");

//For centre alignment Large Text
cmd[1] = 97;
cmd[2] |= 1;
mService.write(cmd);
cmd[0] = 0x1b;
cmd[1] = 0x21;
cmd[2] = 0x10;
mService.write(cmd);
mService.sendMessage("" + center_txt.getText().toString(), "GBK");

//For Left alignment
cmd[0] = 27;
cmd[1] = 97;
cmd[2] |= 1;
mService.write(cmd);
cmd[0] = 27;
cmd[1] = 97;
cmd[2] = 0;
mService.write(cmd);
mService.sendMessage("" + left_txt.getText().toString(), "GBK");



If anybody can help me with the right alignment code, it would be helpful.




1 Answer
1



I faced same issue before but I did not found any solution so, I am using below method as a solution,



Example,


String largetext = "Title";
String largeFontMessage = getWhiteSpace(24 - largetext.length()) + largetext;
String smalltext = "Message";
String smallFontMessage = getWhiteSpace(42 - smalltext.length()) + smalltext;

// print largeFontMessage;
// print smallFontMessage;

public static String getWhiteSpace(int size) {
StringBuilder builder = new StringBuilder(size);
for (int i = 0; i < size; i++) {
builder.append(' ');
}
return builder.toString();
}



In my case in single line small text contains 42 characters and large text contains 24 characters.



Thanks.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV