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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21361919/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:17:30  来源:igfitidea点击:

How can I print JasperPrint directly to printer?

javajasper-reports

提问by michdraft

I use Javato create report with JasperReports. What i want to do is that user be able to print directly, without print dialog.
I create JasperPrintand I know name and model of my printer.

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

I have also looked in the sample herebut could not figure out how.
I use Java 1.7and latest JasperReportslibrary.

我也在这里查看了示例,但无法弄清楚如何。
我使用Java 1.7和最新的JasperReports库。

Does anyone know how to do it?

有谁知道怎么做?

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

采纳答案by michdraft

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

回答by Juan carlos Sanchez

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");
    }
}