简体   繁体   English

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

[英]How can I print JasperPrint directly to printer?

I use Java to create report with JasperReports . 我使用Java通过JasperReports创建报告。 What i want to do is that user be able to print directly, without print dialog. 我想要做的是该用户能够直接打印,而无需打印对话框。
I create JasperPrint and I know name and model of my printer. 我创建了JasperPrint,并且知道打印机的名称和型号。

I have also looked in the sample here but could not figure out how. 我也在这里查看了示例,但不知道如何操作。
I use Java 1.7 and latest JasperReports library. 我使用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();
    }
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将JasperPrint直接流式传输到PDF - Stream JasperPrint directly to PDF 如何在 Android 中的蓝牙打印机上打印图像? - How can I print an image on a Bluetooth printer in Android? 我可以在打印机上使用 FIleOutputStream 进行打印吗 - Can i print using FIleOutputStream on a printer 如何在Java中直接在网络打印机上打印文件。在运行应用程序的系统中未配置打印机? - How to print a file directly on network printer in java.. Printer is not configured in system where application is running? 如何通过打印机(Java SE)在Java表单中打印字段和标签中的总和值 - How Can I Print Sum Values in Fields and Labels in a Java Form Via a Printer (Java SE) 如何在jasper Reports中按组将数据表打印到特定打印机? - how i can print data table to specific printer by group by in jasper Reports? How can I print a label with FBPL and Java on Brother Label Printer (TD-4750TNWB) in a Linux environment? - How can I print a label with FBPL and Java on Brother Label Printer (TD-4750TNWB) in a Linux environment? 使用Java,如何让用户不使用打印对话框就可以设置打印机选项? - Using Java, how can I let users set the printer options without using the print dialog? Android - 如何在蓝牙打印机 (X330) 上打印图像(光栅)? - Android - How can I print an image (Raster) on a Bluetooth printer (X330)? 如何在打印机中打印标签 - How to print tab in printer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM