KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > print > JRPrinterAWT


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.print;
29
30 import java.awt.Graphics JavaDoc;
31 import java.awt.Image JavaDoc;
32 import java.awt.image.BufferedImage JavaDoc;
33 import java.awt.print.Book JavaDoc;
34 import java.awt.print.PageFormat JavaDoc;
35 import java.awt.print.Paper JavaDoc;
36 import java.awt.print.Printable JavaDoc;
37 import java.awt.print.PrinterException JavaDoc;
38 import java.awt.print.PrinterJob JavaDoc;
39 import java.lang.reflect.InvocationTargetException JavaDoc;
40 import java.lang.reflect.Method JavaDoc;
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 /**
52  * @author Teodor Danciu (teodord@users.sourceforge.net)
53  * @version $Id: JRPrinterAWT.java 1422 2006-10-06 19:00:11 +0300 (Fri, 06 Oct 2006) lucianc $
54  */

55 public class JRPrinterAWT implements Printable JavaDoc
56 {
57
58
59     /**
60      *
61      */

62     private JasperPrint jasperPrint = null;
63     private int pageOffset = 0;
64
65
66     /**
67      *
68      */

69     protected JRPrinterAWT(JasperPrint jrPrint) throws JRException
70     {
71         JRGraphEnvInitializer.initializeGraphEnv();
72         
73         jasperPrint = jrPrint;
74     }
75
76
77     /**
78      *
79      */

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     /**
97      *
98      */

99     public static Image JavaDoc 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     /**
111      *
112      */

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 JavaDoc printJob = PrinterJob.getPrinterJob();
138
139         // fix for bug ID 6255588 from Sun bug database
140
initPrinterJobFields(printJob);
141         
142         PageFormat JavaDoc pageFormat = printJob.defaultPage();
143         Paper JavaDoc 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 JavaDoc book = new Book JavaDoc();
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 JavaDoc ex)
199         {
200             throw new JRException("Error printing report.", ex);
201         }
202
203         return isOK;
204     }
205
206
207     /**
208      *
209      */

210     public int print(Graphics JavaDoc graphics, PageFormat JavaDoc pageFormat, int pageIndex) throws PrinterException JavaDoc
211     {
212         if (Thread.currentThread().isInterrupted())
213         {
214             throw new PrinterException JavaDoc("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 JavaDoc(pageIndex));
230             exporter.exportReport();
231         }
232         catch (JRException e)
233         {
234             e.printStackTrace();
235             throw new PrinterException JavaDoc(e.getMessage());
236         }
237
238         return Printable.PAGE_EXISTS;
239     }
240
241
242     /**
243      *
244      */

245     private Image JavaDoc printPageToImage(int pageIndex, float zoom) throws JRException
246     {
247         Image JavaDoc pageImage = new BufferedImage JavaDoc(
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 JavaDoc(pageIndex));
257         exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO, new Float JavaDoc(zoom));
258         exporter.exportReport();
259
260         return pageImage;
261     }
262
263
264     /**
265      * Fix for bug ID 6255588 from Sun bug database
266      * @param job print job that the fix applies to
267      */

268     public static void initPrinterJobFields(PrinterJob JavaDoc job)
269     {
270         Class JavaDoc klass = job.getClass();
271         try {
272             Class JavaDoc printServiceClass = Class.forName("javax.print.PrintService");
273             Method JavaDoc method = klass.getMethod("getPrintService", (Class JavaDoc[])null);
274             Object JavaDoc printService = method.invoke(job, (Object JavaDoc[])null);
275             method = klass.getMethod("setPrintService", new Class JavaDoc[]{printServiceClass});
276             method.invoke(job, new Object JavaDoc[] {printService});
277         } catch (NoSuchMethodException JavaDoc e) {
278         } catch (IllegalAccessException JavaDoc e) {
279         } catch (InvocationTargetException JavaDoc e) {
280         } catch (ClassNotFoundException JavaDoc 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