我只想在不选择用户的情况下打印JasperReport。我进行了搜索,但是没有有效的解决方案。这是我的代码的相关部分:
//compile to .jasper String report = JasperCompileManager.compileReportToFile(sourceFileName); //fill the report JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameter, dataSource); //print the report JasperPrintManager.printReport(jasperPrint, true);
我想选择一台打印机,而不是简单的printReport。有什么办法吗?
这是应该的样子:
try { String report = JasperCompileManager.compileReportToFile(sourceFileName); JasperPrint jasperPrint = JasperFillManager.fillReport(report, para, ds); PrinterJob printerJob = PrinterJob.getPrinterJob(); PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage(); printerJob.defaultPage(pageFormat); int selectedService = 0; AttributeSet attributeSet = new HashPrintServiceAttributeSet(new PrinterName(printerNameShort, null)); PrintService[] printService = PrintServiceLookup.lookupPrintServices(null, attributeSet); try { printerJob.setPrintService(printService[selectedService]); } catch (Exception e) { System.out.println(e); } JRPrintServiceExporter exporter; PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet(); printRequestAttributeSet.add(MediaSizeName.NA_LETTER); printRequestAttributeSet.add(new Copies(1)); // these are deprecated exporter = new JRPrintServiceExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, printService[selectedService]); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printService[selectedService].getAttributes()); exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE); exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE); exporter.exportReport(); } catch (JRException e) { e.printStackTrace(); }