我必须编写一个代码来生成包含图像的 Jasper 报告。
我想将碧 Jade 报告发送到打印机。
我尝试了一个代码:

    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();

但它给了我错误:
   net.sf.jasperreports.engine.JRException: Error loading object from file : C:\Template\Alcon_Ele_Temp1.jrxml

最佳答案

您必须加载 jasper 打印而不是 jasper xml。有一个页面可能可以帮助您 http://jasperreports.sourceforge.net/sample.reference/printservice/index.html

关于java - 如何以编程方式打印 Jasper 报告,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13245964/

10-10 13:31