使用printReport()时选择打印机

时间:2014-06-12 07:45:49

标签: java printing jasper-reports

我安装了很多打印机,我希望能够在不使用对话框的情况下打印任何一台报告。

所以,例如,我有:

JasperPrintManager.printReport(jasperPrint,false);

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

1 个答案:

答案 0 :(得分:0)

这是我在其他网站上找到的解决方案: http://europa.estadistica.unam.mx/jasperreports/dist/docs/sample.reference/printservice/ 它运作得很好:

 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));
  }