我正在考虑如何将文本格式化为居中并使其字体更大。我测试了很多,不知道它是如何工作的......
这是我的代码
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}。它给了我很多不同的结果......需要帮助......在此先感谢,欣赏它......
对不起,我的回答很晚。但是,它可能会在以后帮助其他人。我使用这个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);