我正在考虑如何格式化文本以居中并使其字体变大。我测试了很多,却不知道它是如何工作的...
这是我的代码
byte[] center = new byte[]{0x1B, 'a', 0x01};
byte[] bold = new byte[]{0x1B,0x21,0x08};
我将这些应用到这里
outputStream.write(center);
outputStream.write(header1.getBytes());
outputStream.write(header2.getBytes());
outputStream.write(header3.getBytes());
outputStream.write(header4.getBytes());
outputStream.write(bold);
outputStream.write(dot.getBytes());
outputStream.write(txnNo.getBytes());
outputStream.write(name.getBytes());
outputStream.write(amount.getBytes());
outputStream.write(Date.getBytes());
outputStream.write(Users.getBytes());
outputStream.write(center);
outputStream.write(company.getBytes());
outputStream.write(space.getBytes());
outputStream.write(space.getBytes());
实际上,我希望标题大于常规文本并对齐中心。但我一直在更改此示例{0x1B,0x21,0x08}。它给了我很多不同的结果...需要帮助...在此先感谢,谢谢...
答案 0 :(得分:0)
对不起,我的回答来晚了。但是,以后可能会对其他人有所帮助。我使用了library这对我来说很难获得正确的文本对齐方式,大小和样式设置。最后,我能够弄清楚。我创建了下面的两种方法来分别打印和添加新行。
protected void printConfig(String bill, int size, int style, int align)
{
//size 1 = large, size 2 = medium, size 3 = small
//style 1 = Regular, style 2 = Bold
//align 0 = left, align 1 = center, align 2 = right
try{
byte[] format = new byte[]{27,33, 0};
byte[] change = new byte[]{27,33, 0};
outputStream.write(format);
//different sizes, same style Regular
if (size==1 && style==1) //large
{
change[2] = (byte) (0x10); //large
outputStream.write(change);
}else if(size==2 && style==1) //medium
{
//nothing to change, uses the default settings
}else if(size==3 && style==1) //small
{
change[2] = (byte) (0x3); //small
outputStream.write(change);
}
//different sizes, same style Bold
if (size==1 && style==2) //large
{
change[2] = (byte) (0x10 | 0x8); //large
outputStream.write(change);
}else if(size==2 && style==2) //medium
{
change[2] = (byte) (0x8);
outputStream.write(change);
}else if(size==3 && style==2) //small
{
change[2] = (byte) (0x3 | 0x8); //small
outputStream.write(change);
}
switch (align) {
case 0:
//left align
outputStream.write(PrinterCommands.ESC_ALIGN_LEFT);
break;
case 1:
//center align
outputStream.write(PrinterCommands.ESC_ALIGN_CENTER);
break;
case 2:
//right align
outputStream.write(PrinterCommands.ESC_ALIGN_RIGHT);
break;
}
outputStream.write(bill.getBytes());
outputStream.write(PrinterCommands.LF);
}catch(Exception ex){
Log.e("error", ex.toString());
}
}
protected void printNewLine() {
try {
outputStream.write(PrinterCommands.FEED_LINE);
} catch (IOException e) {
e.printStackTrace();
}
}
下面的PrinterCommands类只是与上述方法有关的简短部分。
public class PrinterCommands {
public static final byte LF = 0x0A;
public static byte[] FEED_LINE = {10};
public static final byte[] ESC_ALIGN_LEFT = new byte[] { 0x1b, 'a', 0x00 };
public static final byte[] ESC_ALIGN_RIGHT = new byte[] { 0x1b, 'a', 0x02 };
public static final byte[] ESC_ALIGN_CENTER = new byte[] { 0x1b, 'a', 0x01 };
public static final byte[] ESC_CANCEL_BOLD = new byte[] { 0x1B, 0x45, 0 };
}
要打印大字体,粗体和居中字体,请按照以下示例调用printConfig方法
printConfig(header1.getBytes(),1,2,1);