KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > PdfEncryptApp


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 import java.io.File JavaDoc;
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 /**
43  * @author Teodor Danciu (teodord@users.sourceforge.net)
44  * @version $Id: PdfEncryptApp.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
45  */

46 public class PdfEncryptApp
47 {
48
49
50     /**
51      *
52      */

53     private static final String JavaDoc TASK_FILL = "fill";
54     private static final String JavaDoc TASK_PDF = "pdf";
55     
56     
57     /**
58      *
59      */

60     public static void main(String JavaDoc[] args)
61     {
62         String JavaDoc fileName = null;
63         String JavaDoc 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 JavaDoc sourceFile = new File JavaDoc(fileName);
94         
95                 JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile);
96         
97                 File JavaDoc destFile = new File JavaDoc(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 JavaDoc(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 JavaDoc e)
129         {
130             e.printStackTrace();
131             System.exit(1);
132         }
133     }
134
135
136     /**
137      *
138      */

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