下面是具體的打印機進行打印碧玉報告一個簡單的解決方案 創建選擇打印機&打印報告
private void PrintReportToPrinter(JasperPrint jp) throws JRException {
// TODO Auto-generated method stub
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
// printRequestAttributeSet.add(MediaSizeName.ISO_A4); //setting page size
printRequestAttributeSet.add(new Copies(1));
PrinterName printerName = new PrinterName("Microsoft XPS Document Writer", null); //gets printer
PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet();
printServiceAttributeSet.add(printerName);
JRPrintServiceExporter exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
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.FALSE);
exporter.exportReport();
}
一個方法然後調用這個方法就像
/* your code*/
Map parameters = new HashMap();
parameters.put("ckotid", kid);
try {
JasperDesign jsd = JRXmlLoader.load("report\\bill\\check_kot.jrxml");
JasperReport jr = JasperCompileManager.compileReport(jsd);
JasperPrint jp = JasperFillManager.fillReport(jr, parameters, con);
//JasperPrintManager.printPage(jp, 0, false);
//JasperPrint jp =reportEngine.fillReport() ;//it returns stream
PrintReportToPrinter(jp);//call method
我不能是唯一一個與此問題所困擾...... –