如何将JasperPrint直接打印到打印机?

时间:2014-01-26 09:42:20

标签: java jasper-reports

我使用 Java 创建带有 JasperReports 的报告。我想要做的是用户能够直接打印,无需打印对话框 我创建 JasperPrint ,我知道我的打印机的名称和型号。

我也看过sample here,但无法弄清楚如何 我使用 Java 1.7 和最新的 JasperReports 库。

有谁知道怎么做?

public class PrintApp  {

    public static void print() {
        JasperPrint jasperPrint = getJasperPrint();
            String printername = AllPrinter.getDepartmentPrinter("Admin");
             // where should i introduce my printer name to jasperreports?
            JasperPrintManager.printReport(jasperPrint, false);
    }

    private static JasperPrint getJasperPrint() {
            return JasperPrinterCreator.getJasperprint();
    }
}

2 个答案:

答案 0 :(得分:4)

我解决了以下问题,希望它可以帮助其他人。

public class PrintApp  {

    public static void print() {
            JasperPrint jasperPrint = getJasperPrint();
            String selectedPrinter = AllPrinter.getDepartmentPrinter("Admin");

            PrinterJob printerJob = PrinterJob.getPrinterJob();
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            PrintService selectedService = null;

            if(services.length != 0 || services != null)
            {
                for(PrintService service : services){
                    String existingPrinter = service.getName().toLowerCase();
                    if(existingPrinter.equals(selectedPrinter))
                    {
                        selectedService = service;
                        break;
                    }
                }

                if(selectedService != null)
                {
                    printerJob.setPrintService(selectedService);
                    boolean printSucceed = JasperPrintManager.printReport(mainPrint, false);
                }
    }

    private static JasperPrint getJasperPrint() {
            return JasperPrinterCreator.getJasperprint();
    }
}

答案 1 :(得分:0)

private void PrintReportToPrinter(JasperPrint jp, String impresora) {
    try {
        // 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(impresora, 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();
    } catch (JRException ex) {
         JOptionPane.showMessageDialog(null,"Cancelo Impresion");
    }
}