1 28 29 import java.text.SimpleDateFormat ; 30 import java.util.Date ; 31 32 import javax.print.attribute.HashPrintRequestAttributeSet ; 33 import javax.print.attribute.HashPrintServiceAttributeSet ; 34 import javax.print.attribute.PrintRequestAttributeSet ; 35 import javax.print.attribute.PrintServiceAttributeSet ; 36 import javax.print.attribute.standard.MediaSizeName ; 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 63 public class PrintServiceApp 64 { 65 66 67 70 private static final String TASK_FILL = "fill"; 71 private static final String TASK_PRINT = "print"; 72 73 74 77 public static void main(String [] args) 78 { 79 String fileName = null; 80 String 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 printRequestAttributeSet = new HashPrintRequestAttributeSet (); 112 printRequestAttributeSet.add(MediaSizeName.ISO_A4); 113 114 PrintServiceAttributeSet printServiceAttributeSet = new HashPrintServiceAttributeSet (); 115 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 e) 143 { 144 e.printStackTrace(); 145 System.exit(1); 146 } 147 } 148 149 150 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 164 private static JasperPrint getJasperPrint() throws JRException 165 { 166 JasperPrint jasperPrint = new JasperPrint(); 168 jasperPrint.setName("NoReport"); 169 jasperPrint.setPageWidth(595); 170 jasperPrint.setPageHeight(842); 171 172 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 ("EEE, MMM d, yyyy")).format(new Date ())); 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 |