[英]How can I print JasperPrint directly to printer?
我使用Java通過JasperReports創建報告。 我想要做的是該用戶能夠直接打印,而無需打印對話框。
我創建了JasperPrint,並且知道打印機的名稱和型號。
我也在這里查看了示例,但不知道如何操作。
我使用Java 1.7和最新的JasperReports庫。
有人知道怎么做嗎?
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();
}
}
我如下解決了它,希望它對其他人有幫助。
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.