如何在指定的打印机上打印 Jasper Reports? [英] How to print Jasper Reports in a specified printer?
本文介绍了如何在指定的打印机上打印 Jasper Reports?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要的只是在用户不选择打印机的情况下打印 JasperReport.我搜索了它,但没有好的解决方案.这是我的代码的相关部分:
All I want, is to print a JasperReport without user selecting a printer. I searched for it but there is no good solution that works. This is the relevat part of my code:
//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.有没有办法做到这一点?
Instead of simple printReport i want to select a printer. is there any way to do this?
推荐答案
它应该是这样的:
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();
}
这篇关于如何在指定的打印机上打印 Jasper Reports?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文