我在操作系统的默认打印机上打印jasper报告。但我需要检查报告是否正确打印(未取消且没有错误)。
要打印报告,请使用以下代码:
private void printOnPrinter(JasperPrint jasperPrint, String printer) throws JRException {
PrintServiceAttributeSet psas = new HashPrintServiceAttributeSet();
psas.add(new PrinterName(printer, null));
JRPrintServiceExporter jrpse = new JRPrintServiceExporter();
jrpse.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
jrpse.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, psas);
jrpse.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
jrpse.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);
try {
jrpse.exportReport();
} catch (JRException e) {
e.printStackTrace();
throw e;
}
}
我尝试使用此代码进行检查,但始终返回成功:
Boolean[] printStatus = jrpse.getPrintStatus();
for (Boolean status : printStatus) {
if (!status) {
System.err.println("Not correctly printed");
}
}