1 28 package net.sf.jasperreports.engine.print; 29 30 import java.awt.Graphics ; 31 import java.awt.Image ; 32 import java.awt.image.BufferedImage ; 33 import java.awt.print.Book ; 34 import java.awt.print.PageFormat ; 35 import java.awt.print.Paper ; 36 import java.awt.print.Printable ; 37 import java.awt.print.PrinterException ; 38 import java.awt.print.PrinterJob ; 39 import java.lang.reflect.InvocationTargetException ; 40 import java.lang.reflect.Method ; 41 42 import net.sf.jasperreports.engine.JRException; 43 import net.sf.jasperreports.engine.JRExporterParameter; 44 import net.sf.jasperreports.engine.JRReport; 45 import net.sf.jasperreports.engine.JasperPrint; 46 import net.sf.jasperreports.engine.export.JRGraphics2DExporter; 47 import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter; 48 import net.sf.jasperreports.engine.util.JRGraphEnvInitializer; 49 50 51 55 public class JRPrinterAWT implements Printable 56 { 57 58 59 62 private JasperPrint jasperPrint = null; 63 private int pageOffset = 0; 64 65 66 69 protected JRPrinterAWT(JasperPrint jrPrint) throws JRException 70 { 71 JRGraphEnvInitializer.initializeGraphEnv(); 72 73 jasperPrint = jrPrint; 74 } 75 76 77 80 public static boolean printPages( 81 JasperPrint jrPrint, 82 int firstPageIndex, 83 int lastPageIndex, 84 boolean withPrintDialog 85 ) throws JRException 86 { 87 JRPrinterAWT printer = new JRPrinterAWT(jrPrint); 88 return printer.printPages( 89 firstPageIndex, 90 lastPageIndex, 91 withPrintDialog 92 ); 93 } 94 95 96 99 public static Image printPageToImage( 100 JasperPrint jrPrint, 101 int pageIndex, 102 float zoom 103 ) throws JRException 104 { 105 JRPrinterAWT printer = new JRPrinterAWT(jrPrint); 106 return printer.printPageToImage(pageIndex, zoom); 107 } 108 109 110 113 private boolean printPages( 114 int firstPageIndex, 115 int lastPageIndex, 116 boolean withPrintDialog 117 ) throws JRException 118 { 119 boolean isOK = true; 120 121 if ( 122 firstPageIndex < 0 || 123 firstPageIndex > lastPageIndex || 124 lastPageIndex >= jasperPrint.getPages().size() 125 ) 126 { 127 throw new JRException( 128 "Invalid page index range : " + 129 firstPageIndex + " - " + 130 lastPageIndex + " of " + 131 jasperPrint.getPages().size() 132 ); 133 } 134 135 pageOffset = firstPageIndex; 136 137 PrinterJob printJob = PrinterJob.getPrinterJob(); 138 139 initPrinterJobFields(printJob); 141 142 PageFormat pageFormat = printJob.defaultPage(); 143 Paper paper = pageFormat.getPaper(); 144 145 printJob.setJobName("JasperReports - " + jasperPrint.getName()); 146 147 switch (jasperPrint.getOrientation()) 148 { 149 case JRReport.ORIENTATION_LANDSCAPE : 150 { 151 pageFormat.setOrientation(PageFormat.LANDSCAPE); 152 paper.setSize(jasperPrint.getPageHeight(), jasperPrint.getPageWidth()); 153 paper.setImageableArea( 154 0, 155 0, 156 jasperPrint.getPageHeight(), 157 jasperPrint.getPageWidth() 158 ); 159 break; 160 } 161 case JRReport.ORIENTATION_PORTRAIT : 162 default : 163 { 164 pageFormat.setOrientation(PageFormat.PORTRAIT); 165 paper.setSize(jasperPrint.getPageWidth(), jasperPrint.getPageHeight()); 166 paper.setImageableArea( 167 0, 168 0, 169 jasperPrint.getPageWidth(), 170 jasperPrint.getPageHeight() 171 ); 172 } 173 } 174 175 pageFormat.setPaper(paper); 176 177 Book book = new Book (); 178 book.append(this, pageFormat, lastPageIndex - firstPageIndex + 1); 179 printJob.setPageable(book); 180 try 181 { 182 if (withPrintDialog) 183 { 184 if (printJob.printDialog()) 185 { 186 printJob.print(); 187 } 188 else 189 { 190 isOK = false; 191 } 192 } 193 else 194 { 195 printJob.print(); 196 } 197 } 198 catch (Exception ex) 199 { 200 throw new JRException("Error printing report.", ex); 201 } 202 203 return isOK; 204 } 205 206 207 210 public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException 211 { 212 if (Thread.currentThread().isInterrupted()) 213 { 214 throw new PrinterException ("Current thread interrupted."); 215 } 216 217 pageIndex += pageOffset; 218 219 if ( pageIndex < 0 || pageIndex >= jasperPrint.getPages().size() ) 220 { 221 return Printable.NO_SUCH_PAGE; 222 } 223 224 try 225 { 226 JRGraphics2DExporter exporter = new JRGraphics2DExporter(); 227 exporter.setParameter(JRExporterParameter.JASPER_PRINT, this.jasperPrint); 228 exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, graphics); 229 exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer (pageIndex)); 230 exporter.exportReport(); 231 } 232 catch (JRException e) 233 { 234 e.printStackTrace(); 235 throw new PrinterException (e.getMessage()); 236 } 237 238 return Printable.PAGE_EXISTS; 239 } 240 241 242 245 private Image printPageToImage(int pageIndex, float zoom) throws JRException 246 { 247 Image pageImage = new BufferedImage ( 248 (int)(jasperPrint.getPageWidth() * zoom) + 1, 249 (int)(jasperPrint.getPageHeight() * zoom) + 1, 250 BufferedImage.TYPE_INT_RGB 251 ); 252 253 JRGraphics2DExporter exporter = new JRGraphics2DExporter(); 254 exporter.setParameter(JRExporterParameter.JASPER_PRINT, this.jasperPrint); 255 exporter.setParameter(JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage.getGraphics()); 256 exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer (pageIndex)); 257 exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float (zoom)); 258 exporter.exportReport(); 259 260 return pageImage; 261 } 262 263 264 268 public static void initPrinterJobFields(PrinterJob job) 269 { 270 Class klass = job.getClass(); 271 try { 272 Class printServiceClass = Class.forName("javax.print.PrintService"); 273 Method method = klass.getMethod("getPrintService", (Class [])null); 274 Object printService = method.invoke(job, (Object [])null); 275 method = klass.getMethod("setPrintService", new Class []{printServiceClass}); 276 method.invoke(job, new Object [] {printService}); 277 } catch (NoSuchMethodException e) { 278 } catch (IllegalAccessException e) { 279 } catch (InvocationTargetException e) { 280 } catch (ClassNotFoundException e) { 281 } 282 } 283 284 285 public static long getImageSize(JasperPrint jasperPrint, float zoom) 286 { 287 int width = (int) (jasperPrint.getPageWidth() * zoom) + 1; 288 int height = (int) (jasperPrint.getPageHeight() * zoom) + 1; 289 return width * height; 290 } 291 } 292 | Popular Tags |