KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pdfbox > pdmodel > encryption > AccessPermission


1 /**
2  * Copyright (c) 2003-2006, www.pdfbox.org
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * 3. Neither the name of pdfbox; nor the names of its
14  * contributors may be used to endorse or promote products derived from this
15  * software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
24  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * http://www.pdfbox.org
29  *
30  */

31
32 package org.pdfbox.pdmodel.encryption;
33
34 /**
35  * This class represents the access permissions to a document.
36  * These permissions are specified in the PDF format specifications, they include:
37  * <ul>
38  * <li>print the document</li>
39  * <li>modify the content of the document</li>
40  * <li>copy or extract content of the document</li>
41  * <li>add or modify annotations</li>
42  * <li>fill in interactive form fields</li>
43  * <li>extract text and graphics for accessibility to visually impaired people</li>
44  * <li>assemble the document</li>
45  * <li>print in degraded quality</li>
46  * </ul>
47  *
48  * This class can be used to protect a document by assigning access permissions to recipients.
49  * In this case, it must be used with a specific ProtectionPolicy.
50  *
51  *
52  * When a document is decrypted, it has a currentAccessPermission property which is the access permissions
53  * granted to the user who decrypted the document.
54  *
55  * @see ProtectionPolicy
56  * @see org.pdfbox.pdmodel.PDDocument#getCurrentAccessPermission()
57  *
58  * @author <a HREF="mailto:ben@benlitchfield.com">Ben Litchfield</a>
59  * @author Benoit Guillon (benoit.guillon@snv.jussieu.fr)
60  *
61  * @version $Revision: 1.3 $
62  */

63
64 public class AccessPermission
65 {
66     
67     private static final int DEFAULT_PERMISSIONS = 0xFFFFFFFF ^ 3;//bits 0 & 1 need to be zero
68
private static final int PRINT_BIT = 3;
69     private static final int MODIFICATION_BIT = 4;
70     private static final int EXTRACT_BIT = 5;
71     private static final int MODIFY_ANNOTATIONS_BIT = 6;
72     private static final int FILL_IN_FORM_BIT = 9;
73     private static final int EXTRACT_FOR_ACCESSIBILITY_BIT = 10;
74     private static final int ASSEMBLE_DOCUMENT_BIT = 11;
75     private static final int DEGRADED_PRINT_BIT = 12;
76     
77     private int bytes = DEFAULT_PERMISSIONS;
78     
79     private boolean readOnly = false;
80     
81     private boolean isPermissionBitOn( int bit )
82     {
83         return (bytes & (1 << (bit-1))) != 0;
84     }
85
86     private boolean setPermissionBit( int bit, boolean value )
87     {
88         int permissions = bytes;
89         if( value )
90         {
91             permissions = permissions | (1 << (bit-1));
92         }
93         else
94         {
95             permissions = permissions & (0xFFFFFFFF ^ (1 << (bit-1)));
96         }
97         bytes = permissions;
98
99         return (bytes & (1 << (bit-1))) != 0;
100     }
101     
102     
103     /**
104      * Create a new access permission object from a byte array.
105      * Bytes are ordered most significant byte first.
106      *
107      * @param b the bytes as defined in PDF specs
108      */

109     
110     public AccessPermission(byte[] b)
111     {
112         bytes = 0;
113         bytes |= b[0] & 0xFF;
114         bytes <<= 8;
115         bytes |= b[1] & 0xFF;
116         bytes <<= 8;
117         bytes |= b[2] & 0xFF;
118         bytes <<= 8;
119         bytes |= b[3] & 0xFF;
120     }
121     
122     /**
123      * This will tell if the access permission corresponds to owner
124      * access permission (no restriction).
125      *
126      * @return true if the access permission does not restrict the use of the document
127      */

128     public boolean isOwnerPermission()
129     {
130         return (this.canAssembleDocument()
131                 && this.canExtractContent()
132                 && this.canExtractForAccessibility()
133                 && this.canFillInForm()
134                 && this.canModify()
135                 && this.canModifyAnnotations()
136                 && this.canPrint()
137                 && this.canPrintDegraded()
138                 );
139     }
140     
141     /**
142      * Create a new access permission object.
143      * By default, all permissions are granted.
144      */

145     public AccessPermission()
146     {
147         bytes = DEFAULT_PERMISSIONS;
148     }
149     
150     /**
151      * returns an access permission object for a document owner.
152      *
153      * @return A standard owner access permission set.
154      */

155     
156     public static AccessPermission getOwnerAccessPermission()
157     {
158         AccessPermission ret = new AccessPermission();
159         ret.setCanAssembleDocument(true);
160         ret.setCanExtractContent(true);
161         ret.setCanExtractForAccessibility(true);
162         ret.setCanFillInForm(true);
163         ret.setCanModify(true);
164         ret.setCanModifyAnnotations(true);
165         ret.setCanPrint(true);
166         ret.setCanPrintDegraded(true);
167         return ret;
168     }
169     
170     /**
171      * This returns an integer representing the access permissions.
172      * This integer can be used for public key encryption. This format
173      * is not documented in the PDF specifications but is necessary for compatibility
174      * with Adobe Acrobat and Adobe Reader.
175      *
176      * @return the integer representing access permissions
177      */

178     
179     public int getPermissionBytesForPublicKey()
180     {
181         setPermissionBit(1, true);
182         setPermissionBit(7, false);
183         setPermissionBit(8, false);
184         for(int i=13; i<=32; i++)
185         {
186             setPermissionBit(i, false);
187         }
188         return bytes;
189     }
190     
191     /**
192      * The returns an integer representing the access permissions.
193      * This integer can be used for standard PDF encryption as specified
194      * in the PDF specifications.
195      *
196      * @return the integer representing the access permissions
197      */

198     public int getPermissionBytes()
199     {
200         return bytes;
201     }
202     
203     /**
204      * This will tell if the user can print.
205      *
206      * @return true If supplied with the user password they are allowed to print.
207      */

208     public boolean canPrint()
209     {
210         return isPermissionBitOn( PRINT_BIT );
211     }
212
213     /**
214      * Set if the user can print.
215      * This method will have no effect if the object is in read only mode
216      *
217      * @param allowPrinting A boolean determining if the user can print.
218      */

219     public void setCanPrint( boolean allowPrinting )
220     {
221         if(!readOnly)
222         {
223             setPermissionBit( PRINT_BIT, allowPrinting );
224         }
225     }
226
227     /**
228      * This will tell if the user can modify contents of the document.
229      *
230      * @return true If supplied with the user password they are allowed to modify the document
231      */

232     public boolean canModify()
233     {
234         return isPermissionBitOn( MODIFICATION_BIT );
235     }
236
237     /**
238      * Set if the user can modify the document.
239      * This method will have no effect if the object is in read only mode
240      *
241      * @param allowModifications A boolean determining if the user can modify the document.
242      */

243     public void setCanModify( boolean allowModifications )
244     {
245         if(!readOnly)
246         {
247             setPermissionBit( MODIFICATION_BIT, allowModifications );
248         }
249     }
250
251     /**
252      * This will tell if the user can extract text and images from the PDF document.
253      *
254      * @return true If supplied with the user password they are allowed to extract content
255      * from the PDF document
256      */

257     public boolean canExtractContent()
258     {
259         return isPermissionBitOn( EXTRACT_BIT );
260     }
261
262     /**
263      * Set if the user can extract content from the document.
264      * This method will have no effect if the object is in read only mode
265      *
266      * @param allowExtraction A boolean determining if the user can extract content
267      * from the document.
268      */

269     public void setCanExtractContent( boolean allowExtraction )
270     {
271         if(!readOnly)
272         {
273             setPermissionBit( EXTRACT_BIT, allowExtraction );
274         }
275     }
276
277     /**
278      * This will tell if the user can add/modify text annotations, fill in interactive forms fields.
279      *
280      * @return true If supplied with the user password they are allowed to modify annotations.
281      */

282     public boolean canModifyAnnotations()
283     {
284         return isPermissionBitOn( MODIFY_ANNOTATIONS_BIT );
285     }
286
287     /**
288      * Set if the user can modify annotations.
289      * This method will have no effect if the object is in read only mode
290      *
291      * @param allowAnnotationModification A boolean determining if the user can modify annotations.
292      */

293     public void setCanModifyAnnotations( boolean allowAnnotationModification )
294     {
295         if(!readOnly)
296         {
297             setPermissionBit( MODIFY_ANNOTATIONS_BIT, allowAnnotationModification );
298         }
299     }
300
301     /**
302      * This will tell if the user can fill in interactive forms.
303      *
304      * @return true If supplied with the user password they are allowed to fill in form fields.
305      */

306     public boolean canFillInForm()
307     {
308         return isPermissionBitOn( FILL_IN_FORM_BIT );
309     }
310
311     /**
312      * Set if the user can fill in interactive forms.
313      * This method will have no effect if the object is in read only mode
314      *
315      * @param allowFillingInForm A boolean determining if the user can fill in interactive forms.
316      */

317     public void setCanFillInForm( boolean allowFillingInForm )
318     {
319         if(!readOnly)
320         {
321             setPermissionBit( FILL_IN_FORM_BIT, allowFillingInForm );
322         }
323     }
324
325     /**
326      * This will tell if the user can extract text and images from the PDF document
327      * for accessibility purposes.
328      *
329      * @return true If supplied with the user password they are allowed to extract content
330      * from the PDF document
331      */

332     public boolean canExtractForAccessibility()
333     {
334         return isPermissionBitOn( EXTRACT_FOR_ACCESSIBILITY_BIT );
335     }
336
337     /**
338      * Set if the user can extract content from the document for accessibility purposes.
339      * This method will have no effect if the object is in read only mode
340      *
341      * @param allowExtraction A boolean determining if the user can extract content
342      * from the document.
343      */

344     public void setCanExtractForAccessibility( boolean allowExtraction )
345     {
346         if(!readOnly)
347         {
348             setPermissionBit( EXTRACT_FOR_ACCESSIBILITY_BIT, allowExtraction );
349         }
350     }
351
352     /**
353      * This will tell if the user can insert/rotate/delete pages.
354      *
355      * @return true If supplied with the user password they are allowed to extract content
356      * from the PDF document
357      */

358     public boolean canAssembleDocument()
359     {
360         return isPermissionBitOn( ASSEMBLE_DOCUMENT_BIT );
361     }
362
363     /**
364      * Set if the user can insert/rotate/delete pages.
365      * This method will have no effect if the object is in read only mode
366      *
367      * @param allowAssembly A boolean determining if the user can assemble the document.
368      */

369     public void setCanAssembleDocument( boolean allowAssembly )
370     {
371         if(!readOnly)
372         {
373             setPermissionBit( ASSEMBLE_DOCUMENT_BIT, allowAssembly );
374         }
375     }
376
377     /**
378      * This will tell if the user can print the document in a degraded format.
379      *
380      * @return true If supplied with the user password they are allowed to print the
381      * document in a degraded format.
382      */

383     public boolean canPrintDegraded()
384     {
385         return isPermissionBitOn( DEGRADED_PRINT_BIT );
386     }
387
388     /**
389      * Set if the user can print the document in a degraded format.
390      * This method will have no effect if the object is in read only mode
391      *
392      * @param allowAssembly A boolean determining if the user can print the
393      * document in a degraded format.
394      */

395     public void setCanPrintDegraded( boolean allowAssembly )
396     {
397         if(!readOnly)
398         {
399             setPermissionBit( DEGRADED_PRINT_BIT, allowAssembly );
400         }
401     }
402     
403     /**
404      * Locks the access permission read only (ie, the setters will have no effects).
405      * After that, the object cannot be unlocked.
406      * This method is used for the currentAccessPermssion of a document to avoid
407      * users to change access permission.
408      */

409     public void setReadOnly()
410     {
411         readOnly = true;
412     }
413     
414     /**
415      * This will tell if the object has been set as read only.
416      *
417      * @return true if the object is in read only mode.
418      */

419     
420     public boolean isReadOnly()
421     {
422         return readOnly;
423     }
424 }
425
Popular Tags