1 28 import java.io.File ; 29 30 import net.sf.jasperreports.engine.JREmptyDataSource; 31 import net.sf.jasperreports.engine.JRException; 32 import net.sf.jasperreports.engine.JRExporterParameter; 33 import net.sf.jasperreports.engine.JasperFillManager; 34 import net.sf.jasperreports.engine.JasperPrint; 35 import net.sf.jasperreports.engine.export.JRPdfExporter; 36 import net.sf.jasperreports.engine.export.JRPdfExporterParameter; 37 import net.sf.jasperreports.engine.util.JRLoader; 38 39 import com.lowagie.text.pdf.PdfWriter; 40 41 42 46 public class PdfEncryptApp 47 { 48 49 50 53 private static final String TASK_FILL = "fill"; 54 private static final String TASK_PDF = "pdf"; 55 56 57 60 public static void main(String [] args) 61 { 62 String fileName = null; 63 String taskName = null; 64 65 if(args.length == 0) 66 { 67 usage(); 68 return; 69 } 70 71 int k = 0; 72 while ( args.length > k ) 73 { 74 if ( args[k].startsWith("-T") ) 75 taskName = args[k].substring(2); 76 if ( args[k].startsWith("-F") ) 77 fileName = args[k].substring(2); 78 79 k++; 80 } 81 82 try 83 { 84 long start = System.currentTimeMillis(); 85 if (TASK_FILL.equals(taskName)) 86 { 87 JasperFillManager.fillReportToFile(fileName, null, new JREmptyDataSource()); 88 System.err.println("Filling time : " + (System.currentTimeMillis() - start)); 89 System.exit(0); 90 } 91 else if (TASK_PDF.equals(taskName)) 92 { 93 File sourceFile = new File (fileName); 94 95 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); 96 97 File destFile = new File (sourceFile.getParent(), jasperPrint.getName() + ".pdf"); 98 99 JRPdfExporter exporter = new JRPdfExporter(); 100 101 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 102 exporter.setParameter(JRExporterParameter.OUTPUT_FILE, destFile); 103 exporter.setParameter(JRPdfExporterParameter.IS_ENCRYPTED, Boolean.TRUE); 104 exporter.setParameter(JRPdfExporterParameter.IS_128_BIT_KEY, Boolean.TRUE); 105 exporter.setParameter(JRPdfExporterParameter.USER_PASSWORD, "jasper"); 106 exporter.setParameter(JRPdfExporterParameter.OWNER_PASSWORD, "reports"); 107 exporter.setParameter( 108 JRPdfExporterParameter.PERMISSIONS, 109 new Integer (PdfWriter.AllowCopy | PdfWriter.AllowPrinting) 110 ); 111 112 exporter.exportReport(); 113 114 System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); 115 System.exit(0); 116 } 117 else 118 { 119 usage(); 120 System.exit(0); 121 } 122 } 123 catch (JRException e) 124 { 125 e.printStackTrace(); 126 System.exit(1); 127 } 128 catch (Exception e) 129 { 130 e.printStackTrace(); 131 System.exit(1); 132 } 133 } 134 135 136 139 private static void usage() 140 { 141 System.out.println( "PdfEncryptApp usage:" ); 142 System.out.println( "\tjava PdfEncryptApp -Ttask -Ffile" ); 143 System.out.println( "\tTasks : fill | pdf" ); 144 } 145 146 147 } 148 | Popular Tags |