java jasperprint,如何将JasperPrint直接打印到打印机?

本文介绍如何使用Java的JasperReports库创建报表并在Java 1.7环境下,避免弹出打印对话框,直接根据用户指定的打印机进行打印。通过获取部门打印机并设置PrintService,实现定制化打印。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 JasperPrint and I know name and model of my printer.

I have also looked in the sample here but could not figure out how.

I use Java 1.7 and latest JasperReports library.

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

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值