您好,我正在用 java 创建一个应用程序,在其中我命令将图形打印到打印机。我遇到的问题是,如果我没有在 Windows 中将打印机设置为默认打印机,它就不会打印到该打印机。
我已经通过名称看到它被发送到打印机打印,但我不知道如何实现它。
这是我目前打印的代码:
PrinterJob job = PrinterJob.getPrinterJob();
int numero = Integer.parseInt(SNumero.getValue().toString());
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(numero));
aset.add(new MediaPrintableArea(0, 0, 62, 29, MediaPrintableArea.MM));
aset.add(Chromaticity.COLOR);
aset.add(OrientationRequested.PORTRAIT);
job.setPrintable(new ObjetoDeImpresion());
job.setJobName("nombre_de_impresion");
try {
job.print(aset);
} catch (PrinterException ex) {
System.out.println(ex);
}
我尝试通过以下方式发送打印机的名称,但它不起作用:
String printerName = "Brother QL-800 (Copiar 1)";
aset.add(new PrinterName(printerName, null));
有谁知道该怎么做?
这里我给你留下一个示例函数,它通过引入一个带有我们要查找的打印机名称的字符串来返回一个 PrintService 类型的对象。
在这里,我留下了几个 关于 PrintService 的 java 文档的链接