Java 打印 API 向打印机发送命令
Java print API send commands to printer
我的电脑上安装了一些打印机 (Windows 7) 并在控制面板上显示良好。
我想发送一些特殊命令,例如:剪切命令或条形码打印命令(ESC/POS 个命令)。
是否可以使用 Java Print API 来做到这一点?还是 Java Print API 只执行打印服务?
提前致谢。
问题已解决:
感谢 VGR。
这里有一个代码可以帮助遇到同样问题的人:
private PrintService printer = ...; // init this using PrintService.lookupPrintServices();
if(this.printer != null) {
String commandToSend = "Some command\n";
Doc myDoc = new SimpleDoc(commandToSend.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
DocPrintJob job = this.printer.createPrintJob();
try {
job.print(myDoc, null);
} catch (PrintException e) {
e.printStackTrace();
}
}
不确定您是否能够解决它,但这里有一个例子
final byte[] VALIDATION_MODE = new byte[]{27, 'c', '0', 4}; // Print in validation mode
final byte[] PAPER_FULL_CUT = {0x1d,0x56,0x00}; // Full cut paper
final byte[] PAPER_PART_CUT = {0x1d,0x56,0x01}; // Partial cut paper
public void print(String receiptContent, String printerName) throws IOException {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob docPrintJob = selectedPrinter(printerName).createPrintJob();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(VALIDATION_MODE);
outputStream.write(receiptContent.getBytes());
outputStream.close();
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
Doc doc = new SimpleDoc(inputStream, flavor, null);
try {
docPrintJob.print(doc, null);
} catch (PrintException e) {
System.out.println("Error:" + e.getMessage());
}
System.out.println("Print Job Finished");
}
我的电脑上安装了一些打印机 (Windows 7) 并在控制面板上显示良好。
我想发送一些特殊命令,例如:剪切命令或条形码打印命令(ESC/POS 个命令)。
是否可以使用 Java Print API 来做到这一点?还是 Java Print API 只执行打印服务?
提前致谢。
问题已解决: 感谢 VGR。
这里有一个代码可以帮助遇到同样问题的人:
private PrintService printer = ...; // init this using PrintService.lookupPrintServices();
if(this.printer != null) {
String commandToSend = "Some command\n";
Doc myDoc = new SimpleDoc(commandToSend.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
DocPrintJob job = this.printer.createPrintJob();
try {
job.print(myDoc, null);
} catch (PrintException e) {
e.printStackTrace();
}
}
不确定您是否能够解决它,但这里有一个例子
final byte[] VALIDATION_MODE = new byte[]{27, 'c', '0', 4}; // Print in validation mode
final byte[] PAPER_FULL_CUT = {0x1d,0x56,0x00}; // Full cut paper
final byte[] PAPER_PART_CUT = {0x1d,0x56,0x01}; // Partial cut paper
public void print(String receiptContent, String printerName) throws IOException {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob docPrintJob = selectedPrinter(printerName).createPrintJob();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(VALIDATION_MODE);
outputStream.write(receiptContent.getBytes());
outputStream.close();
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
Doc doc = new SimpleDoc(inputStream, flavor, null);
try {
docPrintJob.print(doc, null);
} catch (PrintException e) {
System.out.println("Error:" + e.getMessage());
}
System.out.println("Print Job Finished");
}