Java 使用printReport()时选择打印机

Java 使用printReport()时选择打印机,java,printing,jasper-reports,Java,Printing,Jasper Reports,我安装了很多打印机,我希望能够在其中任何一台上打印报告,而无需使用对话框 例如,我有: JasperPrintManager.printReport(jasperPrint,false) 但这只是打印到默认打印机,对吗?我需要能够指定要打印到的机器上安装的打印机的名称。以下是我在另一个网站上找到的解决方案: 它工作得很好: public void print() throws JRException { long start = System.currentTimeMillis(); Pr

我安装了很多打印机,我希望能够在其中任何一台上打印报告,而无需使用对话框

例如,我有:

JasperPrintManager.printReport(jasperPrint,false)


但这只是打印到默认打印机,对吗?我需要能够指定要打印到的机器上安装的打印机的名称。

以下是我在另一个网站上找到的解决方案: 它工作得很好:

 public void print() throws JRException
  {
long start = System.currentTimeMillis();
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
printRequestAttributeSet.add(MediaSizeName.ISO_A4);

PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
//printServiceAttributeSet.add(new PrinterName("Epson Stylus 820 ESC/P 2", null));
//printServiceAttributeSet.add(new PrinterName("hp LaserJet 1320 PCL 6", null));
//printServiceAttributeSet.add(new PrinterName("PDFCreator", null));

JRPrintServiceExporter exporter = new JRPrintServiceExporter();

exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, "build/reports/PrintServiceReport.jrprint");
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);

exporter.exportReport();

System.err.println("Printing time : " + (System.currentTimeMillis() - start));
  }

谢谢你的回答,但这不是我想要的。我想在没有打印对话框的情况下开始打印之前选择打印机。有
configuration.setDisplayPrintDialog(true)将其设置为false,不应该有对话框,应该使用给定的打印机打印。我有一个较旧的版本v4.5,此代码不工作(我找不到SimplePrintServiceExporterConfiguration类)。很好,我找到了我需要的。谢谢