001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.engine.print;
029:
030: import java.awt.Graphics;
031: import java.awt.Image;
032: import java.awt.image.BufferedImage;
033: import java.awt.print.Book;
034: import java.awt.print.PageFormat;
035: import java.awt.print.Paper;
036: import java.awt.print.Printable;
037: import java.awt.print.PrinterException;
038: import java.awt.print.PrinterJob;
039: import java.lang.reflect.InvocationTargetException;
040: import java.lang.reflect.Method;
041:
042: import net.sf.jasperreports.engine.JRException;
043: import net.sf.jasperreports.engine.JRExporterParameter;
044: import net.sf.jasperreports.engine.JRReport;
045: import net.sf.jasperreports.engine.JasperPrint;
046: import net.sf.jasperreports.engine.export.JRGraphics2DExporter;
047: import net.sf.jasperreports.engine.export.JRGraphics2DExporterParameter;
048: import net.sf.jasperreports.engine.util.JRGraphEnvInitializer;
049:
050: /**
051: * @author Teodor Danciu (teodord@users.sourceforge.net)
052: * @version $Id: JRPrinterAWT.java 1422 2006-10-06 16:00:11Z lucianc $
053: */
054: public class JRPrinterAWT implements Printable {
055:
056: /**
057: *
058: */
059: private JasperPrint jasperPrint = null;
060: private int pageOffset = 0;
061:
062: /**
063: *
064: */
065: protected JRPrinterAWT(JasperPrint jrPrint) throws JRException {
066: JRGraphEnvInitializer.initializeGraphEnv();
067:
068: jasperPrint = jrPrint;
069: }
070:
071: /**
072: *
073: */
074: public static boolean printPages(JasperPrint jrPrint,
075: int firstPageIndex, int lastPageIndex,
076: boolean withPrintDialog) throws JRException {
077: JRPrinterAWT printer = new JRPrinterAWT(jrPrint);
078: return printer.printPages(firstPageIndex, lastPageIndex,
079: withPrintDialog);
080: }
081:
082: /**
083: *
084: */
085: public static Image printPageToImage(JasperPrint jrPrint,
086: int pageIndex, float zoom) throws JRException {
087: JRPrinterAWT printer = new JRPrinterAWT(jrPrint);
088: return printer.printPageToImage(pageIndex, zoom);
089: }
090:
091: /**
092: *
093: */
094: private boolean printPages(int firstPageIndex, int lastPageIndex,
095: boolean withPrintDialog) throws JRException {
096: boolean isOK = true;
097:
098: if (firstPageIndex < 0 || firstPageIndex > lastPageIndex
099: || lastPageIndex >= jasperPrint.getPages().size()) {
100: throw new JRException("Invalid page index range : "
101: + firstPageIndex + " - " + lastPageIndex + " of "
102: + jasperPrint.getPages().size());
103: }
104:
105: pageOffset = firstPageIndex;
106:
107: PrinterJob printJob = PrinterJob.getPrinterJob();
108:
109: // fix for bug ID 6255588 from Sun bug database
110: initPrinterJobFields(printJob);
111:
112: PageFormat pageFormat = printJob.defaultPage();
113: Paper paper = pageFormat.getPaper();
114:
115: printJob.setJobName("JasperReports - " + jasperPrint.getName());
116:
117: switch (jasperPrint.getOrientation()) {
118: case JRReport.ORIENTATION_LANDSCAPE: {
119: pageFormat.setOrientation(PageFormat.LANDSCAPE);
120: paper.setSize(jasperPrint.getPageHeight(), jasperPrint
121: .getPageWidth());
122: paper.setImageableArea(0, 0, jasperPrint.getPageHeight(),
123: jasperPrint.getPageWidth());
124: break;
125: }
126: case JRReport.ORIENTATION_PORTRAIT:
127: default: {
128: pageFormat.setOrientation(PageFormat.PORTRAIT);
129: paper.setSize(jasperPrint.getPageWidth(), jasperPrint
130: .getPageHeight());
131: paper.setImageableArea(0, 0, jasperPrint.getPageWidth(),
132: jasperPrint.getPageHeight());
133: }
134: }
135:
136: pageFormat.setPaper(paper);
137:
138: Book book = new Book();
139: book.append(this , pageFormat, lastPageIndex - firstPageIndex
140: + 1);
141: printJob.setPageable(book);
142: try {
143: if (withPrintDialog) {
144: if (printJob.printDialog()) {
145: printJob.print();
146: } else {
147: isOK = false;
148: }
149: } else {
150: printJob.print();
151: }
152: } catch (Exception ex) {
153: throw new JRException("Error printing report.", ex);
154: }
155:
156: return isOK;
157: }
158:
159: /**
160: *
161: */
162: public int print(Graphics graphics, PageFormat pageFormat,
163: int pageIndex) throws PrinterException {
164: if (Thread.currentThread().isInterrupted()) {
165: throw new PrinterException("Current thread interrupted.");
166: }
167:
168: pageIndex += pageOffset;
169:
170: if (pageIndex < 0 || pageIndex >= jasperPrint.getPages().size()) {
171: return Printable.NO_SUCH_PAGE;
172: }
173:
174: try {
175: JRGraphics2DExporter exporter = new JRGraphics2DExporter();
176: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
177: this .jasperPrint);
178: exporter
179: .setParameter(
180: JRGraphics2DExporterParameter.GRAPHICS_2D,
181: graphics);
182: exporter.setParameter(JRExporterParameter.PAGE_INDEX,
183: new Integer(pageIndex));
184: exporter.exportReport();
185: } catch (JRException e) {
186: e.printStackTrace();
187: throw new PrinterException(e.getMessage());
188: }
189:
190: return Printable.PAGE_EXISTS;
191: }
192:
193: /**
194: *
195: */
196: private Image printPageToImage(int pageIndex, float zoom)
197: throws JRException {
198: Image pageImage = new BufferedImage((int) (jasperPrint
199: .getPageWidth() * zoom) + 1, (int) (jasperPrint
200: .getPageHeight() * zoom) + 1,
201: BufferedImage.TYPE_INT_RGB);
202:
203: JRGraphics2DExporter exporter = new JRGraphics2DExporter();
204: exporter.setParameter(JRExporterParameter.JASPER_PRINT,
205: this .jasperPrint);
206: exporter.setParameter(
207: JRGraphics2DExporterParameter.GRAPHICS_2D, pageImage
208: .getGraphics());
209: exporter.setParameter(JRExporterParameter.PAGE_INDEX,
210: new Integer(pageIndex));
211: exporter.setParameter(JRGraphics2DExporterParameter.ZOOM_RATIO,
212: new Float(zoom));
213: exporter.exportReport();
214:
215: return pageImage;
216: }
217:
218: /**
219: * Fix for bug ID 6255588 from Sun bug database
220: * @param job print job that the fix applies to
221: */
222: public static void initPrinterJobFields(PrinterJob job) {
223: Class klass = job.getClass();
224: try {
225: Class printServiceClass = Class
226: .forName("javax.print.PrintService");
227: Method method = klass.getMethod("getPrintService",
228: (Class[]) null);
229: Object printService = method.invoke(job, (Object[]) null);
230: method = klass.getMethod("setPrintService",
231: new Class[] { printServiceClass });
232: method.invoke(job, new Object[] { printService });
233: } catch (NoSuchMethodException e) {
234: } catch (IllegalAccessException e) {
235: } catch (InvocationTargetException e) {
236: } catch (ClassNotFoundException e) {
237: }
238: }
239:
240: public static long getImageSize(JasperPrint jasperPrint, float zoom) {
241: int width = (int) (jasperPrint.getPageWidth() * zoom) + 1;
242: int height = (int) (jasperPrint.getPageHeight() * zoom) + 1;
243: return width * height;
244: }
245: }
|