来自Java的Jasper打印没有Dialog

时间:2015-10-20 05:04:49

标签: java printing

String Report = "C:\\Template\\"+file_name+".jrxml";//my Jasper report file
JasperPrint print = JasperFillManager.fillReport(Report,null,con);
PrinterJob job = PrinterJob.getPrinterJob();
/* Create an array of PrintServices */
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
int selectedService = 0;
/* Scan found services to see if anyone suits our needs *
for(int i = 0; i < services.length;i++)
{
    if(services[i].getName().toUpperCase().contains("Your printer's name"))
    {
        /*If the service is named as what we are querying we select it */
             selectedService = i;
    }
}
job.setPrintService(services[selectedService]);
PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
MediaSizeName mediaSizeName = MediaSize.findMedia(4,4,MediaPrintableArea.INCH);
printRequestAttributeSet.add(mediaSizeName);
printRequestAttributeSet.add(new Copies(1));
JRPrintServiceExporter exporter;
exporter = new JRPrintServiceExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
/* We set the selected service and pass it as a paramenter */
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[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.TRUE);
exporter.exportReport();

使用上面的代码从java打印jasper。如果我执行上述操作,打印工作正常,它打开AWT对话框,其中设置了默认值,可能来自系统打印机(只有一个默认打印机)。我想通过更改为exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.FALSE);

来避免打开对话框

但是当我这样做时,它忽略了页面布局,大小,边距,它需要一些默认值(可能是A4大小)。如何纠正这个?

0 个答案:

没有答案