How can I print JasperPrint directly to printer?

后端 未结 2 1976
星月不相逢
星月不相逢 2020-12-18 14:04

I use Java to create report with JasperReports. What i want to do is that user be able to print directly, without print dialog.
I create JasperPri

相关标签:
2条回答
  • 2020-12-18 14:46

    I solved it as below, hopefully it helps someone else.

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-18 14:53
    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");
        }
    }
    
    0 讨论(0)
提交回复
热议问题