【问题标题】:Need to know to code printer name in Java需要知道在 Java 中编码打印机名称
【发布时间】:2024-01-23 01:37:01
【问题描述】:

我使用 jasper 报告在 Java 中创建报告,大多数时候我尝试使用代码设置打印机名称,但我做不到。请帮助我如何在不使用打印管理器的情况下为打印机提供打印名称

try {
        String comname = "";
        comname = "Stars Bombay Sweet (Pvt) Ltd.";

        JRTableModelDataSource dts = new JRTableModelDataSource(jTable1.getModel());
        String path = "D:\\Aster Soft\\Stars Reports\\posh_print\\report1.jrxml";
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("name", comname);

        params.put("para1", InvNo.getText());
        params.put("para2", dat);
        params.put("para3", Customer.getText());
        params.put("credit", jComboBox1.getSelectedItem());

        params.put("Qty", GrossQty.getText());
        params.put("TotAmont", TotalAmount.getText());
        params.put("Discount", Discount.getText());
        params.put("GrossAmont", DisAmount.getText());

        JasperReport report = JasperCompileManager.compileReport(path);
        JasperPrint print = JasperFillManager.fillReport(report, params, dts);
        JasperViewer.viewReport(print, false);
        JasperPrintManager.printReport(print, true);
    } catch (Exception e) {
        e.printStackTrace();
    }

【问题讨论】:

    标签: java printing jasper-reports


    【解决方案1】:
            JasperPrint print = JasperFillManager.fillReport(this.class.getResource("/classpath/yourReport.jasper").getPath(),
                new HashMap(), new yourReportDataSource());
            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 &lt; 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]);
    

    我认为下面的博客链接会对你有所帮助

    http://blog.marcnuri.com/choosing-a-printer-programmatically-in-jasper-reports/

    【讨论】: