Print iReport in Java Code

Choosing a printer programmatically in Jasper Reports

JasperPrint print = JasperFillManager.<strong>fillReport</strong>(  this.class.<strong>getResource</strong>("<em>/classpath/yourReport.jasper</em>").<strong>getPath</strong>(),  new <strong>HashMap</strong>(), new <strong>yourReportDataSource</strong>());
 PrinterJob job = PrinterJob.getPrinterJob();
 /* Create an array of PrintServices */
 PrintService[] services = PrintServiceLookup.<strong>lookupPrintServices</strong>(null,  null);
 int selectedService = 0;
 /* Scan found services to see if anyone suits our needs */
 for(int i = 0; i < services.length;i++){
 if(services[i].<strong>getName</strong>().<strong>toUpperCase</strong>().<strong>contains</strong>("<em>Your  printer's name</em>")){
 /*If the service is named as what we are querying we select it */
 selectedService = i;
 PrintRequestAttributeSet printRequestAttributeSet = new <strong>HashPrintRequestAttributeSet</strong>();
 MediaSizeName mediaSizeName = MediaSize.<strong>findMedia</strong>(4,4,MediaPrintableArea.INCH);
 printRequestAttributeSet.<strong>add</strong>(new Copies(1));
 JRPrintServiceExporter exporter;
 exporter = new <strong>JRPrintServiceExporter</strong>();
 exporter.<strong>setParameter</strong>(JRExporterParameter.JASPER_PRINT,  print);
 /* We set the selected service and pass it as a paramenter */
 exporter.<strong>setParameter</strong>(JRPrintServiceExporterParameter.PRINT_SERVICE,  services[selectedService]);
 exporter.<strong>setParameter</strong>(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET,  services[selectedService].getAttributes());
 exporter.<strong>setParameter</strong>(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET,  printRequestAttributeSet);
 exporter.<strong>setParameter</strong>(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG,  Boolean.FALSE);
 exporter.<strong>setParameter</strong>(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG,  Boolean.TRUE);

In the above code you can see how to accomplish our purpose. The first part of all is generating our JasperPrint, this is common to all methods of report printing. Then comes the important part. First we look for the available printServices in the computer. Second, we scan the found services to see if there is anyone that suits our needs. Finally we set the selected print service in the exporter.

You will also find methods to choose the paper output and the number of copies to print.

Reference by:

3 thoughts on “Print iReport in Java Code

  1. It’s a pity you don’t have a donate button! I’d most certainly donate to this brilliant blog!
    I guess for now i’ll settle for bookmarking and adding your RSS feed to my Google account.

    I look forward to new updates and will share this blog with my Facebook group.

    Chat soon!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s