2014-01-26 82 views
2

我使用Java创建报告JasperReports。我想要做的是该用户能够直接打印,而无需打印对话框。
我创建了JasperPrint我知道打印机的名称和型号。如何将JasperPrint直接打印到打印机?

我也看过sample here,但无法弄清楚如何。
我使用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(); 
    } 
} 
+1

这看起来相关: https://community.oracle.com/thread/1758399 – selalerer

回答

3

我解决了这个问题,如下,希望它可以帮助别人。

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

这将客户端的打印机上进行打印? –

+1

您正在打印mainPrint,我认为这应该是jasperPrint? – PhilDin

+0

什么导入用来包含AllPrinter导致它给我一个错误“Allprinter can not be resolved” –

相关问题