我认为有必要使用 kpl 命令稍后通过尝试和失败打印我可以使用这些行进行打印:
public void PrintTicketZ() {
PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
attributeSet.add(OrientationRequested.PORTRAIT);
attributeSet.add(new Copies(1));
attributeSet.add(new JobName("PrintService", null));
attributeSet.add(new MediaPrintableArea(10, 0, 60, 400, MediaPrintableArea.MM));
PrinterJob pj = PrinterJob.getPrinterJob();
pj.setPrintable(new Printable() {
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex == 0) {
int h = 100;
int w = 100;
String company="--";
try {
double x = pf.getImageableX();
double y = pf.getImageableY();
Paper paper = pf.getPaper();
pf.getPaper().setImageableArea(0, 0, paper.getWidth(), paper.getHeight());
Graphics2D g2d = (Graphics2D) g;
g2d.translate(x, y);
g2d.setColor(Color.black);
g2d.setFont(new Font("TimesRoman", Font.PLAIN, 10));
g2d.drawString("FOLIO: ", 105, 40);
g2d.drawString("FECHA: ", 0, 60);
g2d.drawString("HORA: ", 0, 75);
g2d.drawString("N° EMPLEADO: " , 0, 100);
g2d.drawString("EMPRESA: " , 0, 115);
g2d.drawString("EMPLEADO: ", 0, 130);
} catch (WriterException e) {
System.out.println("Error " + e);
}
return Printable.PAGE_EXISTS;
} else {
return Printable.NO_SUCH_PAGE;
}
}
});
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
if (defaultPrintService != null) {
try{
String print_default = defaultPrintService.getName();
pj.setPrintService(defaultPrintService);
pj.print(attributeSet);
log.info("Printing...");
PrintServiceAttributeSet printServiceAttributeSet = defaultPrintService.getAttributes();
String trabajo = printServiceAttributeSet.get(QueuedJobCount.class).toString();
int trabajos = Integer.parseInt(trabajo);
} catch (PrinterException pe) {
log.error("Error al imprimir: " + pe);
System.out.println(pe);
}
}
}