1 28 package net.sf.jasperreports.engine.export; 29 30 import java.awt.Graphics ; 31 import java.awt.print.PageFormat ; 32 import java.awt.print.Printable ; 33 import java.awt.print.PrinterException ; 34 import java.awt.print.PrinterJob ; 35 36 import javax.print.PrintService ; 37 import javax.print.PrintServiceLookup ; 38 import javax.print.attribute.HashPrintRequestAttributeSet ; 39 import javax.print.attribute.HashPrintServiceAttributeSet ; 40 import javax.print.attribute.PrintRequestAttributeSet ; 41 import javax.print.attribute.PrintServiceAttributeSet ; 42 import javax.print.attribute.standard.MediaPrintableArea ; 43 import javax.print.attribute.standard.OrientationRequested ; 44 import javax.print.attribute.standard.PageRanges ; 45 46 import net.sf.jasperreports.engine.JRAbstractExporter; 47 import net.sf.jasperreports.engine.JRException; 48 import net.sf.jasperreports.engine.JRExporterParameter; 49 import net.sf.jasperreports.engine.JRReport; 50 import net.sf.jasperreports.engine.JasperPrint; 51 import net.sf.jasperreports.engine.print.JRPrinterAWT; 52 53 54 58 public class JRPrintServiceExporter extends JRAbstractExporter implements Printable 59 { 60 61 62 65 protected JRGraphics2DExporter exporter = null; 66 protected boolean displayPageDialog = false; 67 protected boolean displayPrintDialog = false; 68 69 protected int reportIndex = 0; 70 71 74 public void exportReport() throws JRException 75 { 76 77 setOffset(); 78 79 try 80 { 81 82 setExportContext(); 83 84 85 setInput(); 86 87 88 if (!isModeBatch) 89 { 90 setPageRange(); 91 } 92 93 PrintServiceAttributeSet printServiceAttributeSet = 94 (PrintServiceAttributeSet )parameters.get(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET); 95 if (printServiceAttributeSet == null) 96 { 97 printServiceAttributeSet = new HashPrintServiceAttributeSet (); 98 } 99 100 Boolean pageDialog = (Boolean )parameters.get(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG); 101 if (pageDialog != null) 102 { 103 displayPageDialog = pageDialog.booleanValue(); 104 } 105 106 Boolean printDialog = (Boolean )parameters.get(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG); 107 if (printDialog != null) 108 { 109 displayPrintDialog = printDialog.booleanValue(); 110 } 111 112 PrinterJob printerJob = PrinterJob.getPrinterJob(); 113 114 JRPrinterAWT.initPrinterJobFields(printerJob); 115 116 printerJob.setPrintable(this); 117 118 PrintService selectedService = (PrintService ) parameters.get(JRPrintServiceExporterParameter.PRINT_SERVICE); 120 if (selectedService == null) { 121 PrintService [] services = PrintServiceLookup.lookupPrintServices(null, printServiceAttributeSet); 122 if (services.length > 0) 123 selectedService = services[0]; 124 } 125 126 if (selectedService == null) 127 { 128 throw new JRException("No suitable print service found."); 129 } 130 131 try 132 { 133 printerJob.setPrintService(selectedService); 134 } 135 catch (PrinterException e) 136 { 137 throw new JRException(e); 138 } 139 140 for(reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++) 142 { 143 jasperPrint = (JasperPrint)jasperPrintList.get(reportIndex); 144 145 exporter = new JRGraphics2DExporter(); 146 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 147 exporter.setParameter(JRExporterParameter.PROGRESS_MONITOR, parameters.get(JRExporterParameter.PROGRESS_MONITOR)); 148 exporter.setParameter(JRExporterParameter.OFFSET_X, parameters.get(JRExporterParameter.OFFSET_X)); 149 exporter.setParameter(JRExporterParameter.OFFSET_Y, parameters.get(JRExporterParameter.OFFSET_Y)); 150 exporter.setParameter(JRExporterParameter.CLASS_LOADER, classLoader); 151 exporter.setParameter(JRExporterParameter.URL_HANDLER_FACTORY, urlHandlerFactory); 152 exporter.setParameter(JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE, parameters.get(JRGraphics2DExporterParameter.MINIMIZE_PRINTER_JOB_SIZE)); 153 154 PrintRequestAttributeSet printRequestAttributeSet = new HashPrintRequestAttributeSet (); 155 PrintRequestAttributeSet printRequestAttributeSetParam = 156 (PrintRequestAttributeSet )parameters.get(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET); 157 if (printRequestAttributeSetParam != null) 158 { 159 printRequestAttributeSet.addAll(printRequestAttributeSetParam); 160 } 161 162 try 163 { 164 if (!printRequestAttributeSet.containsKey(MediaPrintableArea .class)) 165 { 166 int printableWidth; 167 int printableHeight; 168 switch (jasperPrint.getOrientation()) 169 { 170 case JRReport.ORIENTATION_LANDSCAPE: 171 printableWidth = jasperPrint.getPageHeight(); 172 printableHeight = jasperPrint.getPageWidth(); 173 break; 174 default: 175 printableWidth = jasperPrint.getPageWidth(); 176 printableHeight = jasperPrint.getPageHeight(); 177 break; 178 } 179 180 printRequestAttributeSet.add( 181 new MediaPrintableArea ( 182 0f, 183 0f, 184 printableWidth / 72f, 185 printableHeight / 72f, 186 MediaPrintableArea.INCH 187 ) 188 ); 189 } 190 191 if (!printRequestAttributeSet.containsKey(OrientationRequested .class)) 192 { 193 OrientationRequested orientation; 194 switch (jasperPrint.getOrientation()) 195 { 196 case JRReport.ORIENTATION_LANDSCAPE: 197 orientation = OrientationRequested.LANDSCAPE; 198 break; 199 default: 200 orientation = OrientationRequested.PORTRAIT; 201 break; 202 } 203 printRequestAttributeSet.add(orientation); 204 } 205 206 if (!isModeBatch) 207 { 208 printRequestAttributeSet.add(new PageRanges (startPageIndex + 1, endPageIndex + 1)); 209 } 210 211 printerJob.setJobName("JasperReports - " + jasperPrint.getName()); 212 213 if (displayPageDialog) 214 { 215 printerJob.pageDialog(printRequestAttributeSet); 216 } 217 218 if (displayPrintDialog) 219 { 220 if (printerJob.printDialog(printRequestAttributeSet)) 221 { 222 printerJob.print(printRequestAttributeSet); 223 } 224 } 225 else 226 { 227 printerJob.print(printRequestAttributeSet); 228 } 229 } 230 catch (PrinterException e) 231 { 232 throw new JRException(e); 233 } 234 } 235 } 236 finally 237 { 238 resetExportContext(); 239 } 240 } 241 242 243 246 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException 247 { 248 if (Thread.currentThread().isInterrupted()) 249 { 250 throw new PrinterException ("Current thread interrupted."); 251 } 252 253 if ( pageIndex < 0 || pageIndex >= jasperPrint.getPages().size() ) 254 { 255 return Printable.NO_SUCH_PAGE; 256 } 257 258 exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, graphics); 259 exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer (pageIndex)); 260 261 try 262 { 263 exporter.exportReport(); 264 } 265 catch (JRException e) 266 { 267 throw new PrinterException (e.getMessage()); 268 } 269 270 return Printable.PAGE_EXISTS; 271 } 272 273 274 } 275 | Popular Tags |