001: /*
002: /*
003: * Copyright 2002 Paulo Soares
004: *
005: * The contents of this file are subject to the Mozilla Public License Version 1.1
006: * (the "License"); you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
008: *
009: * Software distributed under the License is distributed on an "AS IS" basis,
010: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
011: * for the specific language governing rights and limitations under the License.
012: *
013: * The Original Code is 'iText, a free JAVA-PDF library'.
014: *
015: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
016: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
017: * All Rights Reserved.
018: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
019: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
020: *
021: * Contributor(s): all the names of the contributors are added in the source code
022: * where applicable.
023: *
024: * Alternatively, the contents of this file may be used under the terms of the
025: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
026: * provisions of LGPL are applicable instead of those above. If you wish to
027: * allow use of your version of this file only under the terms of the LGPL
028: * License and not to allow others to use your version of this file under
029: * the MPL, indicate your decision by deleting the provisions above and
030: * replace them with the notice and other provisions required by the LGPL.
031: * If you do not delete the provisions above, a recipient may use your version
032: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
033: *
034: * This library is free software; you can redistribute it and/or modify it
035: * under the terms of the MPL as stated above or under the terms of the GNU
036: * Library General Public License as published by the Free Software Foundation;
037: * either version 2 of the License, or any later version.
038: *
039: * This library is distributed in the hope that it will be useful, but WITHOUT
040: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
041: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
042: * details.
043: *
044: * If you didn't download this code from the following link, you should check if
045: * you aren't using an obsolete version:
046: * http://www.lowagie.com/iText/
047: */
048:
049: package com.lowagie.text.pdf;
050:
051: import java.io.IOException;
052: import java.io.OutputStream;
053: import java.util.HashMap;
054:
055: import com.lowagie.text.DocumentException;
056:
057: /** This class takes any PDF and returns exactly the same but
058: * encrypted. All the content, links, outlines, etc, are kept.
059: * It is also possible to change the info dictionary.
060: */
061: public class PdfEncryptor {
062:
063: private PdfEncryptor() {
064: }
065:
066: /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
067: * <code>PdfWriter</code>. The userPassword and the
068: * ownerPassword can be null or have zero length. In this case the ownerPassword
069: * is replaced by a random string. The open permissions for the document can be
070: * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
071: * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
072: * The permissions can be combined by ORing them.
073: * @param reader the read PDF
074: * @param os the output destination
075: * @param userPassword the user password. Can be null or empty
076: * @param ownerPassword the owner password. Can be null or empty
077: * @param permissions the user permissions
078: * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
079: * @throws DocumentException on error
080: * @throws IOException on error */
081: public static void encrypt(PdfReader reader, OutputStream os,
082: byte userPassword[], byte ownerPassword[], int permissions,
083: boolean strength128Bits) throws DocumentException,
084: IOException {
085: PdfStamper stamper = new PdfStamper(reader, os);
086: stamper.setEncryption(userPassword, ownerPassword, permissions,
087: strength128Bits);
088: stamper.close();
089: }
090:
091: /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
092: * <code>PdfWriter</code>. The userPassword and the
093: * ownerPassword can be null or have zero length. In this case the ownerPassword
094: * is replaced by a random string. The open permissions for the document can be
095: * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
096: * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
097: * The permissions can be combined by ORing them.
098: * @param reader the read PDF
099: * @param os the output destination
100: * @param userPassword the user password. Can be null or empty
101: * @param ownerPassword the owner password. Can be null or empty
102: * @param permissions the user permissions
103: * @param strength128Bits <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
104: * @param newInfo an optional <CODE>String</CODE> map to add or change
105: * the info dictionary. Entries with <CODE>null</CODE>
106: * values delete the key in the original info dictionary
107: * @throws DocumentException on error
108: * @throws IOException on error
109: */
110: public static void encrypt(PdfReader reader, OutputStream os,
111: byte userPassword[], byte ownerPassword[], int permissions,
112: boolean strength128Bits, HashMap newInfo)
113: throws DocumentException, IOException {
114: PdfStamper stamper = new PdfStamper(reader, os);
115: stamper.setEncryption(userPassword, ownerPassword, permissions,
116: strength128Bits);
117: stamper.setMoreInfo(newInfo);
118: stamper.close();
119: }
120:
121: /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
122: * <code>PdfWriter</code>. The userPassword and the
123: * ownerPassword can be null or have zero length. In this case the ownerPassword
124: * is replaced by a random string. The open permissions for the document can be
125: * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
126: * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
127: * The permissions can be combined by ORing them.
128: * @param reader the read PDF
129: * @param os the output destination
130: * @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
131: * @param userPassword the user password. Can be null or empty
132: * @param ownerPassword the owner password. Can be null or empty
133: * @param permissions the user permissions
134: * @throws DocumentException on error
135: * @throws IOException on error */
136: public static void encrypt(PdfReader reader, OutputStream os,
137: boolean strength, String userPassword,
138: String ownerPassword, int permissions)
139: throws DocumentException, IOException {
140: PdfStamper stamper = new PdfStamper(reader, os);
141: stamper.setEncryption(strength, userPassword, ownerPassword,
142: permissions);
143: stamper.close();
144: }
145:
146: /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
147: * <code>PdfWriter</code>. The userPassword and the
148: * ownerPassword can be null or have zero length. In this case the ownerPassword
149: * is replaced by a random string. The open permissions for the document can be
150: * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
151: * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
152: * The permissions can be combined by ORing them.
153: * @param reader the read PDF
154: * @param os the output destination
155: * @param strength <code>true</code> for 128 bit key length, <code>false</code> for 40 bit key length
156: * @param userPassword the user password. Can be null or empty
157: * @param ownerPassword the owner password. Can be null or empty
158: * @param permissions the user permissions
159: * @param newInfo an optional <CODE>String</CODE> map to add or change
160: * the info dictionary. Entries with <CODE>null</CODE>
161: * values delete the key in the original info dictionary
162: * @throws DocumentException on error
163: * @throws IOException on error
164: */
165: public static void encrypt(PdfReader reader, OutputStream os,
166: boolean strength, String userPassword,
167: String ownerPassword, int permissions, HashMap newInfo)
168: throws DocumentException, IOException {
169: PdfStamper stamper = new PdfStamper(reader, os);
170: stamper.setEncryption(strength, userPassword, ownerPassword,
171: permissions);
172: stamper.setMoreInfo(newInfo);
173: stamper.close();
174: }
175:
176: /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
177: * <code>PdfWriter</code>. The userPassword and the
178: * ownerPassword can be null or have zero length. In this case the ownerPassword
179: * is replaced by a random string. The open permissions for the document can be
180: * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
181: * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
182: * The permissions can be combined by ORing them.
183: * @param reader the read PDF
184: * @param os the output destination
185: * @param type the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
186: * Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
187: * @param userPassword the user password. Can be null or empty
188: * @param ownerPassword the owner password. Can be null or empty
189: * @param permissions the user permissions
190: * @param newInfo an optional <CODE>String</CODE> map to add or change
191: * the info dictionary. Entries with <CODE>null</CODE>
192: * values delete the key in the original info dictionary
193: * @throws DocumentException on error
194: * @throws IOException on error
195: */
196: public static void encrypt(PdfReader reader, OutputStream os,
197: int type, String userPassword, String ownerPassword,
198: int permissions, HashMap newInfo) throws DocumentException,
199: IOException {
200: PdfStamper stamper = new PdfStamper(reader, os);
201: stamper.setEncryption(type, userPassword, ownerPassword,
202: permissions);
203: stamper.setMoreInfo(newInfo);
204: stamper.close();
205: }
206:
207: /** Entry point to encrypt a PDF document. The encryption parameters are the same as in
208: * <code>PdfWriter</code>. The userPassword and the
209: * ownerPassword can be null or have zero length. In this case the ownerPassword
210: * is replaced by a random string. The open permissions for the document can be
211: * AllowPrinting, AllowModifyContents, AllowCopy, AllowModifyAnnotations,
212: * AllowFillIn, AllowScreenReaders, AllowAssembly and AllowDegradedPrinting.
213: * The permissions can be combined by ORing them.
214: * @param reader the read PDF
215: * @param os the output destination
216: * @param type the type of encryption. It can be one of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128.
217: * Optionally DO_NOT_ENCRYPT_METADATA can be ored to output the metadata in cleartext
218: * @param userPassword the user password. Can be null or empty
219: * @param ownerPassword the owner password. Can be null or empty
220: * @param permissions the user permissions
221: * values delete the key in the original info dictionary
222: * @throws DocumentException on error
223: * @throws IOException on error
224: */
225: public static void encrypt(PdfReader reader, OutputStream os,
226: int type, String userPassword, String ownerPassword,
227: int permissions) throws DocumentException, IOException {
228: PdfStamper stamper = new PdfStamper(reader, os);
229: stamper.setEncryption(type, userPassword, ownerPassword,
230: permissions);
231: stamper.close();
232: }
233:
234: /**
235: * Give you a verbose analysis of the permissions.
236: * @param permissions the permissions value of a PDF file
237: * @return a String that explains the meaning of the permissions value
238: */
239: public static String getPermissionsVerbose(int permissions) {
240: StringBuffer buf = new StringBuffer("Allowed:");
241: if ((PdfWriter.AllowPrinting & permissions) == PdfWriter.AllowPrinting)
242: buf.append(" Printing");
243: if ((PdfWriter.AllowModifyContents & permissions) == PdfWriter.AllowModifyContents)
244: buf.append(" Modify contents");
245: if ((PdfWriter.AllowCopy & permissions) == PdfWriter.AllowCopy)
246: buf.append(" Copy");
247: if ((PdfWriter.AllowModifyAnnotations & permissions) == PdfWriter.AllowModifyAnnotations)
248: buf.append(" Modify annotations");
249: if ((PdfWriter.AllowFillIn & permissions) == PdfWriter.AllowFillIn)
250: buf.append(" Fill in");
251: if ((PdfWriter.AllowScreenReaders & permissions) == PdfWriter.AllowScreenReaders)
252: buf.append(" Screen readers");
253: if ((PdfWriter.AllowAssembly & permissions) == PdfWriter.AllowAssembly)
254: buf.append(" Assembly");
255: if ((PdfWriter.AllowDegradedPrinting & permissions) == PdfWriter.AllowDegradedPrinting)
256: buf.append(" Degraded printing");
257: return buf.toString();
258: }
259: }
|