KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PrintServiceApp


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
29 import java.text.SimpleDateFormat JavaDoc;
30 import java.util.Date JavaDoc;
31
32 import javax.print.attribute.HashPrintRequestAttributeSet JavaDoc;
33 import javax.print.attribute.HashPrintServiceAttributeSet JavaDoc;
34 import javax.print.attribute.PrintRequestAttributeSet JavaDoc;
35 import javax.print.attribute.PrintServiceAttributeSet JavaDoc;
36 import javax.print.attribute.standard.MediaSizeName JavaDoc;
37
38 import net.sf.jasperreports.engine.JRAlignment;
39 import net.sf.jasperreports.engine.JRException;
40 import net.sf.jasperreports.engine.JRExporterParameter;
41 import net.sf.jasperreports.engine.JRImage;
42 import net.sf.jasperreports.engine.JRImageRenderer;
43 import net.sf.jasperreports.engine.JRPrintImage;
44 import net.sf.jasperreports.engine.JRPrintLine;
45 import net.sf.jasperreports.engine.JRPrintPage;
46 import net.sf.jasperreports.engine.JRPrintText;
47 import net.sf.jasperreports.engine.JasperPrint;
48 import net.sf.jasperreports.engine.base.JRBasePrintImage;
49 import net.sf.jasperreports.engine.base.JRBasePrintLine;
50 import net.sf.jasperreports.engine.base.JRBasePrintPage;
51 import net.sf.jasperreports.engine.base.JRBasePrintText;
52 import net.sf.jasperreports.engine.design.JRDesignStyle;
53 import net.sf.jasperreports.engine.export.JRPrintServiceExporter;
54 import net.sf.jasperreports.engine.export.JRPrintServiceExporterParameter;
55 import net.sf.jasperreports.engine.util.JRLoader;
56 import net.sf.jasperreports.engine.util.JRSaver;
57
58
59 /**
60  * @author Teodor Danciu (teodord@users.sourceforge.net)
61  * @version $Id: PrintServiceApp.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
62  */

63 public class PrintServiceApp
64 {
65
66
67     /**
68      *
69      */

70     private static final String JavaDoc TASK_FILL = "fill";
71     private static final String JavaDoc TASK_PRINT = "print";
72     
73     
74     /**
75      *
76      */

77     public static void main(String JavaDoc[] args)
78     {
79         String JavaDoc fileName = null;
80         String JavaDoc taskName = null;
81
82         if(args.length == 0)
83         {
84             usage();
85             return;
86         }
87                 
88         int k = 0;
89         while ( args.length > k )
90         {
91             if ( args[k].startsWith("-T") )
92                 taskName = args[k].substring(2);
93             if ( args[k].startsWith("-F") )
94                 fileName = args[k].substring(2);
95             
96             k++;
97         }
98
99         try
100         {
101             long start = System.currentTimeMillis();
102             if (TASK_FILL.equals(taskName))
103             {
104                 JasperPrint jasperPrint = getJasperPrint();
105                 JRSaver.saveObject(jasperPrint, fileName);
106                 System.err.println("Filling time : " + (System.currentTimeMillis() - start));
107                 System.exit(0);
108             }
109             else if (TASK_PRINT.equals(taskName))
110             {
111                 PrintRequestAttributeSet JavaDoc printRequestAttributeSet = new HashPrintRequestAttributeSet JavaDoc();
112                 printRequestAttributeSet.add(MediaSizeName.ISO_A4);
113
114                 PrintServiceAttributeSet JavaDoc printServiceAttributeSet = new HashPrintServiceAttributeSet JavaDoc();
115                 //printServiceAttributeSet.add(new PrinterName("Epson Stylus 800 ESC/P 2", null));
116
//printServiceAttributeSet.add(new PrinterName("HP LaserJet 4P", null));
117

118                 JRPrintServiceExporter exporter = new JRPrintServiceExporter();
119                 
120                 exporter.setParameter(JRExporterParameter.INPUT_FILE_NAME, fileName);
121                 exporter.setParameter(JRPrintServiceExporterParameter.PRINT_REQUEST_ATTRIBUTE_SET, printRequestAttributeSet);
122                 exporter.setParameter(JRPrintServiceExporterParameter.PRINT_SERVICE_ATTRIBUTE_SET, printServiceAttributeSet);
123                 exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PAGE_DIALOG, Boolean.FALSE);
124                 exporter.setParameter(JRPrintServiceExporterParameter.DISPLAY_PRINT_DIALOG, Boolean.TRUE);
125                 
126                 exporter.exportReport();
127
128                 System.err.println("Printing time : " + (System.currentTimeMillis() - start));
129                 System.exit(0);
130             }
131             else
132             {
133                 usage();
134                 System.exit(0);
135             }
136         }
137         catch (JRException e)
138         {
139             e.printStackTrace();
140             System.exit(1);
141         }
142         catch (Exception JavaDoc e)
143         {
144             e.printStackTrace();
145             System.exit(1);
146         }
147     }
148
149
150     /**
151      *
152      */

153     private static void usage()
154     {
155         System.out.println( "PrintServiceApp usage:" );
156         System.out.println( "\tjava PrintServiceApp -Ttask -Ffile" );
157         System.out.println( "\tTasks : fill | print" );
158     }
159
160
161     /**
162      *
163      */

164     private static JasperPrint getJasperPrint() throws JRException
165     {
166         //JasperPrint
167
JasperPrint jasperPrint = new JasperPrint();
168         jasperPrint.setName("NoReport");
169         jasperPrint.setPageWidth(595);
170         jasperPrint.setPageHeight(842);
171         
172         //Fonts
173
JRDesignStyle normalStyle = new JRDesignStyle();
174         normalStyle.setName("Arial_Normal");
175         normalStyle.setDefault(true);
176         normalStyle.setFontName("Arial");
177         normalStyle.setFontSize(8);
178         normalStyle.setPdfFontName("Helvetica");
179         normalStyle.setPdfEncoding("Cp1252");
180         normalStyle.setPdfEmbedded(false);
181         jasperPrint.addStyle(normalStyle);
182
183         JRDesignStyle boldStyle = new JRDesignStyle();
184         boldStyle.setName("Arial_Bold");
185         boldStyle.setFontName("Arial");
186         boldStyle.setFontSize(8);
187         boldStyle.setBold(true);
188         boldStyle.setPdfFontName("Helvetica-Bold");
189         boldStyle.setPdfEncoding("Cp1252");
190         boldStyle.setPdfEmbedded(false);
191         jasperPrint.addStyle(boldStyle);
192
193         JRDesignStyle italicStyle = new JRDesignStyle();
194         italicStyle.setName("Arial_Italic");
195         italicStyle.setFontName("Arial");
196         italicStyle.setFontSize(8);
197         italicStyle.setItalic(true);
198         italicStyle.setPdfFontName("Helvetica-Oblique");
199         italicStyle.setPdfEncoding("Cp1252");
200         italicStyle.setPdfEmbedded(false);
201         jasperPrint.addStyle(italicStyle);
202         
203         JRPrintPage page = new JRBasePrintPage();
204
205         JRPrintLine line = new JRBasePrintLine(jasperPrint.getDefaultStyleProvider());
206         line.setX(40);
207         line.setY(50);
208         line.setWidth(515);
209         line.setHeight(0);
210         page.addElement(line);
211         
212         JRPrintImage image = new JRBasePrintImage(jasperPrint.getDefaultStyleProvider());
213         image.setX(45);
214         image.setY(55);
215         image.setWidth(165);
216         image.setHeight(40);
217         image.setScaleImage(JRImage.SCALE_IMAGE_CLIP);
218         image.setRenderer(
219             JRImageRenderer.getInstance(
220                 JRLoader.loadBytesFromLocation("jasperreports.gif")
221                 )
222             );
223         page.addElement(image);
224
225         JRPrintText text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
226         text.setX(210);
227         text.setY(55);
228         text.setWidth(345);
229         text.setHeight(30);
230         text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
231         text.setLineSpacingFactor(1.3133681f);
232         text.setLeadingOffset(-4.955078f);
233         text.setStyle(boldStyle);
234         text.setFontSize(18);
235         text.setText("JasperReports Project Description");
236         page.addElement(text);
237
238         text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
239         text.setX(210);
240         text.setY(85);
241         text.setWidth(325);
242         text.setHeight(15);
243         text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_RIGHT);
244         text.setLineSpacingFactor(1.329241f);
245         text.setLeadingOffset(-4.076172f);
246         text.setStyle(italicStyle);
247         text.setFontSize(12);
248         text.setText((new SimpleDateFormat JavaDoc("EEE, MMM d, yyyy")).format(new Date JavaDoc()));
249         page.addElement(text);
250
251         text = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
252         text.setX(40);
253         text.setY(150);
254         text.setWidth(515);
255         text.setHeight(200);
256         text.setHorizontalAlignment(JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED);
257         text.setLineSpacingFactor(1.329241f);
258         text.setLeadingOffset(-4.076172f);
259         text.setStyle(normalStyle);
260         text.setFontSize(14);
261         text.setText(
262             "JasperReports is a powerful report-generating tool that has the ability to deliver rich content onto the screen, to the printer or into PDF, HTML, XLS, CSV or XML files.\n\n" +
263             "It is entirely written in Java and can be used in a variety of Java enabled applications, including J2EE or Web applications, to generate dynamic content.\n\n" +
264             "Its main purpose is to help creating page oriented, ready to print documents in a simple and flexible manner."
265             );
266         page.addElement(text);
267
268         jasperPrint.addPage(page);
269
270         return jasperPrint;
271     }
272     
273
274 }
275
Popular Tags