Printing Jasper Report to connected printer device in JAVA


Printing Report is one of the major task in all the application.Its the main documenting purpose too.During printing we have to consider one major thing that what are the printers that have been currently connected to the system in order to print those document.Following code may help you to analyse that what are the component that have been currently configured to the system
// Comment
private void printaction() 
    {   Object[] obj = {"Choose the Printer \n\n", jcmb_selectprinter};
        Object stringArray[] = {"Print","Cancel"};
         if (JOptionPane.showOptionDialog(null, obj, "Choose the Printer",
        JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stringArray, obj) == JOptionPane.YES_OPTION)
         {
         String printerne= jcmb_selectprinter.getSelectedItem().toString();
         try
            {

            Connection conn = null;
            HashMap params = new HashMap();
         
      try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            conn = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/yourDB","root","root");
        } 
        catch (SQLException ex) 
        {
            JOptionPane.showMessageDialog(null, ex);
        } 
        catch (ClassNotFoundException ex) 
        {
            JOptionPane.showMessageDialog(null, ex);
        }

            JasperReport report = (JasperReport)
            JRLoader.loadObject(this.getClass().getResource("/report/yourReport.jasper"));
            JasperPrint print = JasperFillManager.fillReport(report, params, conn);
            print.setOrientation(JasperReport.ORIENTATION_PORTRAIT);
            PrinterJob job = PrinterJob.getPrinterJob();
            PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
            int selectedService = 0;
         
            for(int i = 0; i < services.length;i++)
            {
              
            if(services[i].getName().toUpperCase().equals(printerne.toUpperCase()))
            {
            selectedService = i;
            }
            }
            job.setPrintService(services[selectedService]);
            PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet();
            MediaSizeName mediaSizeName = MediaSize.findMedia( Float.parseFloat( String.valueOf(8.264)), Float.parseFloat( String.valueOf(11.694)),MediaPrintableArea.INCH);
            printRequestAttributeSet.add(mediaSizeName);
            printRequestAttributeSet.add(new Copies(1));
            JRPrintServiceExporter exporter;
            exporter = new JRPrintServiceExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE, services[selectedService]);
            exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, services[selectedService].getAttributes());
            exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
            exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
            exporter.exportReport();
        cn.conn.close();
          JOptionPane.showMessageDialog(null,"printed");
            }

        catch (JRException ex)
            {
             
            }
            catch (SQLException ex)
        {
            JOptionPane.showMessageDialog(null,ex);
        }
         catch (PrinterException ex) {
            
            }
              }
        else
         {
            JOptionPane.showMessageDialog(null," canceled your process");
         }
    }

    } 
The above code help us print the selected document directly to the printer.Here in this sample code we have used the jasper report to print the document straightly to print the document.
The following code helps you load the number of printers connected to your system in a jComboBox.


// Comment

       
public void printer()
    {  jcmb_selectprinter.removeAllItems();
        PrintService[] ps = PrintServiceLookup.lookupPrintServices(null, null);
        PrintService[] psdef = PrintServiceLookup.lookupPrintServices(null, null);
        for(int i=0;i<ps.length;i++)
        {
            if(ps[i].getName().equals(psdef[i].getName()))
            {jcmb_selectprinter.addItem(psdef[i].getName());
              
            } else {
                System.out.println(psdef[i].getName());

            }
        }
    }
 

raja P Web Developer

3 comments:

  1. Hi this is jemes,nice information and i am looking for some best Ink Cartridges For Printers
    post where i can get the proper and latest information and its problem.
    please suggest

    Thanks

    ReplyDelete
  2. Efficiency can be guaranteed when printing especially if important guidelines will be followed. Some techniques and applications can easily spare a person from unnecessary waste of time, effort, and money.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...