KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > pdf > PdfEncryptor


1 /*
2 /*
3  * Copyright 2002 Paulo Soares
4  *
5  * The contents of this file are subject to the Mozilla Public License Version 1.1
6  * (the "License"); you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the License.
12  *
13  * The Original Code is 'iText, a free JAVA-PDF library'.
14  *
15  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
16  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
17  * All Rights Reserved.
18  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
19  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
20  *
21  * Contributor(s): all the names of the contributors are added in the source code
22  * where applicable.
23  *
24  * Alternatively, the contents of this file may be used under the terms of the
25  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
26  * provisions of LGPL are applicable instead of those above. If you wish to
27  * allow use of your version of this file only under the terms of the LGPL
28  * License and not to allow others to use your version of this file under
29  * the MPL, indicate your decision by deleting the provisions above and
30  * replace them with the notice and other provisions required by the LGPL.
31  * If you do not delete the provisions above, a recipient may use your version
32  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
33  *
34  * This library is free software; you can redistribute it and/or modify it
35  * under the terms of the MPL as stated above or under the terms of the GNU
36  * Library General Public License as published by the Free Software Foundation;
37  * either version 2 of the License, or any later version.
38  *
39  * This library is distributed in the hope that it will be useful, but WITHOUT
40  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
41  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
42  * details.
43  *
44  * If you didn't download this code from the following link, you should check if
45  * you aren't using an obsolete version:
46  * http://www.lowagie.com/iText/
47  */

48
49 package com.lowagie.text.pdf;
50
51 import java.io.IOException JavaDoc;
52 import java.io.OutputStream JavaDoc;
53 import java.util.HashMap JavaDoc;
54
55 import com.lowagie.text.DocumentException;
56
57 /** This class takes any PDF and returns exactly the same but
58  * encrypted. All the content, links, outlines, etc, are kept.
59  * It is also possible to change the info dictionary.
60  */

61 public class PdfEncryptor {
62     
63     private PdfEncryptor(){
64     }
65     
66     /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
67      * <code>PdfWriter</code>. The userPassword and the
68      * ownerPassword can be null or have zero length. In this case the ownerPassword
69      * is replaced by a random string. The open permissions for the document can be
70      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
71      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
72      * The permissions can be combined by ORing them.
73      * @param reader the read PDF
74      * @param os the output destination
75      * @param userPassword the user password. Can be null or empty
76      * @param ownerPassword the owner password. Can be null or empty
77      * @param permissions the user permissions
78      * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
79      * @throws DocumentException on error
80      * @throws IOException on error */

81     public static void encrypt(PdfReader reader, OutputStream JavaDoc os, byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits) throws DocumentException, IOException JavaDoc {
82         PdfStamper stamper = new PdfStamper(reader, os);
83         stamper.setEncryption(userPassword, ownerPassword, permissions, strength128Bits);
84         stamper.close();
85     }
86     
87     /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
88      * <code>PdfWriter</code>. The userPassword and the
89      * ownerPassword can be null or have zero length. In this case the ownerPassword
90      * is replaced by a random string. The open permissions for the document can be
91      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
92      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
93      * The permissions can be combined by ORing them.
94      * @param reader the read PDF
95      * @param os the output destination
96      * @param userPassword the user password. Can be null or empty
97      * @param ownerPassword the owner password. Can be null or empty
98      * @param permissions the user permissions
99      * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
100      * @param newInfo an optional <CODE>String</CODE> map to add or change
101      * the info dictionary. Entries with <CODE>null</CODE>
102      * values delete the key in the original info dictionary
103      * @throws DocumentException on error
104      * @throws IOException on error
105      */

106     public static void encrypt(PdfReader reader, OutputStream JavaDoc os, byte userPassword[], byte ownerPassword[], int permissions, boolean strength128Bits, HashMap JavaDoc newInfo) throws DocumentException, IOException JavaDoc {
107         PdfStamper stamper = new PdfStamper(reader, os);
108         stamper.setEncryption(userPassword, ownerPassword, permissions, strength128Bits);
109         stamper.setMoreInfo(newInfo);
110         stamper.close();
111     }
112     
113     /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
114      * <code>PdfWriter</code>. The userPassword and the
115      * ownerPassword can be null or have zero length. In this case the ownerPassword
116      * is replaced by a random string. The open permissions for the document can be
117      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
118      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
119      * The permissions can be combined by ORing them.
120      * @param reader the read PDF
121      * @param os the output destination
122      * @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
123      * @param userPassword the user password. Can be null or empty
124      * @param ownerPassword the owner password. Can be null or empty
125      * @param permissions the user permissions
126      * @throws DocumentException on error
127      * @throws IOException on error */

128     public static void encrypt(PdfReader reader, OutputStream JavaDoc os, boolean strength, String JavaDoc userPassword, String JavaDoc ownerPassword, int permissions) throws DocumentException, IOException JavaDoc {
129         PdfStamper stamper = new PdfStamper(reader, os);
130         stamper.setEncryption(strength, userPassword, ownerPassword, permissions);
131         stamper.close();
132     }
133     
134     /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
135      * <code>PdfWriter</code>. The userPassword and the
136      * ownerPassword can be null or have zero length. In this case the ownerPassword
137      * is replaced by a random string. The open permissions for the document can be
138      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
139      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
140      * The permissions can be combined by ORing them.
141      * @param reader the read PDF
142      * @param os the output destination
143      * @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
144      * @param userPassword the user password. Can be null or empty
145      * @param ownerPassword the owner password. Can be null or empty
146      * @param permissions the user permissions
147      * @param newInfo an optional <CODE>String</CODE> map to add or change
148      * the info dictionary. Entries with <CODE>null</CODE>
149      * values delete the key in the original info dictionary
150      * @throws DocumentException on error
151      * @throws IOException on error
152      */

153     public static void encrypt(PdfReader reader, OutputStream JavaDoc os, boolean strength, String JavaDoc userPassword, String JavaDoc ownerPassword, int permissions, HashMap JavaDoc newInfo) throws DocumentException, IOException JavaDoc {
154         PdfStamper stamper = new PdfStamper(reader, os);
155         stamper.setEncryption(strength, userPassword, ownerPassword, permissions);
156         stamper.setMoreInfo(newInfo);
157         stamper.close();
158     }
159
160     
161     /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
162      * <code>PdfWriter</code>. The userPassword and the
163      * ownerPassword can be null or have zero length. In this case the ownerPassword
164      * is replaced by a random string. The open permissions for the document can be
165      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
166      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
167      * The permissions can be combined by ORing them.
168      * @param reader the read PDF
169      * @param os the output destination
170      * @param type the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
171      * Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
172      * @param userPassword the user password. Can be null or empty
173      * @param ownerPassword the owner password. Can be null or empty
174      * @param permissions the user permissions
175      * @param newInfo an optional <CODE>String</CODE> map to add or change
176      * the info dictionary. Entries with <CODE>null</CODE>
177      * values delete the key in the original info dictionary
178      * @throws DocumentException on error
179      * @throws IOException on error
180      */

181     public static void encrypt(PdfReader reader, OutputStream JavaDoc os, int type, String JavaDoc userPassword, String JavaDoc ownerPassword, int permissions, HashMap JavaDoc newInfo) throws DocumentException, IOException JavaDoc {
182         PdfStamper stamper = new PdfStamper(reader, os);
183         stamper.setEncryption(type, userPassword, ownerPassword, permissions);
184         stamper.setMoreInfo(newInfo);
185         stamper.close();
186     }
187     
188     /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
189      * <code>PdfWriter</code>. The userPassword and the
190      * ownerPassword can be null or have zero length. In this case the ownerPassword
191      * is replaced by a random string. The open permissions for the document can be
192      * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
193      * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
194      * The permissions can be combined by ORing them.
195      * @param reader the read PDF
196      * @param os the output destination
197      * @param type the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
198      * Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
199      * @param userPassword the user password. Can be null or empty
200      * @param ownerPassword the owner password. Can be null or empty
201      * @param permissions the user permissions
202      * values delete the key in the original info dictionary
203      * @throws DocumentException on error
204      * @throws IOException on error
205      */

206     public static void encrypt(PdfReader reader, OutputStream JavaDoc os, int type, String JavaDoc userPassword, String JavaDoc ownerPassword, int permissions) throws DocumentException, IOException JavaDoc {
207         PdfStamper stamper = new PdfStamper(reader, os);
208         stamper.setEncryption(type, userPassword, ownerPassword, permissions);
209         stamper.close();
210     }
211     
212     /**
213      * Give you a verbose analysis of the permissions.
214      * @param permissions the permissions value of a PDF file
215      * @return a String that explains the meaning of the permissions value
216      */

217     public static String JavaDoc getPermissionsVerbose(int permissions) {
218         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("Allowed:");
219         if ((PdfWriter.AllowPrinting & permissions) == PdfWriter.AllowPrinting) buf.append(" Printing");
220         if ((PdfWriter.AllowModifyContents & permissions) == PdfWriter.AllowModifyContents) buf.append(" Modify contents");
221         if ((PdfWriter.AllowCopy & permissions) == PdfWriter.AllowCopy) buf.append(" Copy");
222         if ((PdfWriter.AllowModifyAnnotations & permissions) == PdfWriter.AllowModifyAnnotations) buf.append(" Modify annotations");
223         if ((PdfWriter.AllowFillIn & permissions) == PdfWriter.AllowFillIn) buf.append(" Fill in");
224         if ((PdfWriter.AllowScreenReaders & permissions) == PdfWriter.AllowScreenReaders) buf.append(" Screen readers");
225         if ((PdfWriter.AllowAssembly & permissions) == PdfWriter.AllowAssembly) buf.append(" Assembly");
226         if ((PdfWriter.AllowDegradedPrinting & permissions) == PdfWriter.AllowDegradedPrinting) buf.append(" Degraded printing");
227         return buf.toString();
228     }
229 }
Popular Tags