1 28 import java.awt.Color ; 29 import java.io.File ; 30 31 import net.sf.jasperreports.engine.JRDataSource; 32 import net.sf.jasperreports.engine.JRException; 33 import net.sf.jasperreports.engine.JRRectangle; 34 import net.sf.jasperreports.engine.JRStyle; 35 import net.sf.jasperreports.engine.JasperExportManager; 36 import net.sf.jasperreports.engine.JasperFillManager; 37 import net.sf.jasperreports.engine.JasperPrint; 38 import net.sf.jasperreports.engine.JasperPrintManager; 39 import net.sf.jasperreports.engine.JasperReport; 40 import net.sf.jasperreports.engine.util.JRLoader; 41 import net.sf.jasperreports.engine.util.JRSaver; 42 43 44 48 public class AlterDesignApp 49 { 50 51 52 55 private static final String TASK_FILL = "fill"; 56 private static final String TASK_PRINT = "print"; 57 private static final String TASK_PDF = "pdf"; 58 59 60 63 public static void main(String [] args) 64 { 65 String fileName = null; 66 String taskName = null; 67 68 if(args.length == 0) 69 { 70 usage(); 71 return; 72 } 73 74 int k = 0; 75 while ( args.length > k ) 76 { 77 if ( args[k].startsWith("-T") ) 78 taskName = args[k].substring(2); 79 if ( args[k].startsWith("-F") ) 80 fileName = args[k].substring(2); 81 82 k++; 83 } 84 85 try 86 { 87 long start = System.currentTimeMillis(); 88 if (TASK_FILL.equals(taskName)) 89 { 90 File sourceFile = new File (fileName); 91 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(sourceFile); 92 93 JRRectangle rectangle = (JRRectangle)jasperReport.getTitle().getElementByKey("first.rectangle"); 94 rectangle.setForecolor(new Color ((int)(16000000 * Math.random()))); 95 rectangle.setBackcolor(new Color ((int)(16000000 * Math.random()))); 96 97 rectangle = (JRRectangle)jasperReport.getTitle().getElementByKey("second.rectangle"); 98 rectangle.setForecolor(new Color ((int)(16000000 * Math.random()))); 99 rectangle.setBackcolor(new Color ((int)(16000000 * Math.random()))); 100 101 rectangle = (JRRectangle)jasperReport.getTitle().getElementByKey("third.rectangle"); 102 rectangle.setForecolor(new Color ((int)(16000000 * Math.random()))); 103 rectangle.setBackcolor(new Color ((int)(16000000 * Math.random()))); 104 105 JRStyle style = jasperReport.getStyles()[0]; 106 style.setFontSize(16); 107 style.setItalic(true); 108 109 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, (JRDataSource)null); 110 111 File destFile = new File (sourceFile.getParent(), jasperReport.getName() + ".jrprint"); 112 JRSaver.saveObject(jasperPrint, destFile); 113 114 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 115 System.exit(0); 116 } 117 else if (TASK_PRINT.equals(taskName)) 118 { 119 JasperPrintManager.printReport(fileName, true); 120 System.err.println("Printing time : " + (System.currentTimeMillis() - start)); 121 System.exit(0); 122 } 123 else if (TASK_PDF.equals(taskName)) 124 { 125 JasperExportManager.exportReportToPdfFile(fileName); 126 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 127 System.exit(0); 128 } 129 else 130 { 131 usage(); 132 System.exit(0); 133 } 134 } 135 catch (JRException e) 136 { 137 e.printStackTrace(); 138 System.exit(1); 139 } 140 catch (Exception e) 141 { 142 e.printStackTrace(); 143 System.exit(1); 144 } 145 } 146 147 148 151 private static void usage() 152 { 153 System.out.println( "AlterDesignApp usage:" ); 154 System.out.println( "\tjava AlterDesignApp -Ttask -Ffile" ); 155 System.out.println( "\tTasks : fill | print | pdf" ); 156 } 157 158 159 } 160
| Popular Tags
|