1 31 package org.pdfbox.pdmodel.encryption; 32 33 import org.pdfbox.cos.COSDictionary; 34 import org.pdfbox.cos.COSInteger; 35 import org.pdfbox.cos.COSName; 36 import org.pdfbox.cos.COSNumber; 37 import org.pdfbox.cos.COSString; 38 39 import java.io.IOException ; 40 41 50 public class PDStandardEncryption extends PDEncryptionDictionary 51 { 52 55 public static final String FILTER_NAME = "Standard"; 56 57 60 public static final int DEFAULT_REVISION = 3; 61 62 65 public static final int REVISION2 = 2; 66 69 public static final int REVISION3 = 3; 70 73 public static final int REVISION4 = 4; 74 75 78 public static final int DEFAULT_PERMISSIONS = 0xFFFFFFFF ^ 3; 80 private static final int PRINT_BIT = 3; 81 private static final int MODIFICATION_BIT = 4; 82 private static final int EXTRACT_BIT = 5; 83 private static final int MODIFY_ANNOTATIONS_BIT = 6; 84 private static final int FILL_IN_FORM_BIT = 9; 85 private static final int EXTRACT_FOR_ACCESSIBILITY_BIT = 10; 86 private static final int ASSEMBLE_DOCUMENT_BIT = 11; 87 private static final int DEGRADED_PRINT_BIT = 12; 88 89 93 public PDStandardEncryption() 94 { 95 super(); 96 encryptionDictionary.setItem( COSName.FILTER, COSName.getPDFName( FILTER_NAME ) ); 97 setVersion( PDEncryptionDictionary.VERSION1_40_BIT_ALGORITHM ); 98 setRevision( PDStandardEncryption.REVISION2 ); 99 setPermissions( DEFAULT_PERMISSIONS ); 100 } 101 102 107 public PDStandardEncryption( COSDictionary dict ) 108 { 109 super( dict ); 110 } 111 112 118 public int getRevision() 119 { 120 int revision = DEFAULT_VERSION; 121 COSNumber cosRevision = (COSNumber)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "R" ) ); 122 if( cosRevision != null ) 123 { 124 revision = cosRevision.intValue(); 125 } 126 return revision; 127 } 128 129 138 public void setRevision( int revision ) 139 { 140 encryptionDictionary.setItem( COSName.getPDFName( "R" ), new COSInteger( revision ) ); 141 } 142 143 148 public byte[] getOwnerKey() 149 { 150 byte[] o = null; 151 COSString owner = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "O" ) ); 152 if( owner != null ) 153 { 154 o = owner.getBytes(); 155 } 156 return o; 157 } 158 159 166 public void setOwnerKey( byte[] o ) throws IOException 167 { 168 COSString owner = new COSString(); 169 owner.append( o ); 170 encryptionDictionary.setItem( COSName.getPDFName( "O" ), owner ); 171 } 172 173 178 public byte[] getUserKey() 179 { 180 byte[] u = null; 181 COSString user = (COSString)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "U" ) ); 182 if( user != null ) 183 { 184 u = user.getBytes(); 185 } 186 return u; 187 } 188 189 196 public void setUserKey( byte[] u ) throws IOException 197 { 198 COSString user = new COSString(); 199 user.append( u ); 200 encryptionDictionary.setItem( COSName.getPDFName( "U" ), user ); 201 } 202 203 208 public int getPermissions() 209 { 210 int permissions = 0; 211 COSInteger p = (COSInteger)encryptionDictionary.getDictionaryObject( COSName.getPDFName( "P" ) ); 212 if( p != null ) 213 { 214 permissions = p.intValue(); 215 } 216 return permissions; 217 } 218 219 224 public void setPermissions( int p ) 225 { 226 encryptionDictionary.setItem( COSName.getPDFName( "P" ), new COSInteger( p ) ); 227 } 228 229 private boolean isPermissionBitOn( int bit ) 230 { 231 return (getPermissions() & (1 << (bit-1))) != 0; 232 } 233 234 private boolean setPermissionBit( int bit, boolean value ) 235 { 236 int permissions = getPermissions(); 237 if( value ) 238 { 239 permissions = permissions | (1 << (bit-1)); 240 } 241 else 242 { 243 permissions = permissions & (0xFFFFFFFF ^ (1 << (bit-1))); 244 } 245 setPermissions( permissions ); 246 247 return (getPermissions() & (1 << (bit-1))) != 0; 248 } 249 250 255 public boolean canPrint() 256 { 257 return isPermissionBitOn( PRINT_BIT ); 258 } 259 260 265 public void setCanPrint( boolean allowPrinting ) 266 { 267 setPermissionBit( PRINT_BIT, allowPrinting ); 268 } 269 270 275 public boolean canModify() 276 { 277 return isPermissionBitOn( MODIFICATION_BIT ); 278 } 279 280 285 public void setCanModify( boolean allowModifications ) 286 { 287 setPermissionBit( MODIFICATION_BIT, allowModifications ); 288 } 289 290 296 public boolean canExtractContent() 297 { 298 return isPermissionBitOn( EXTRACT_BIT ); 299 } 300 301 307 public void setCanExtractContent( boolean allowExtraction ) 308 { 309 setPermissionBit( EXTRACT_BIT, allowExtraction ); 310 } 311 312 317 public boolean canModifyAnnotations() 318 { 319 return isPermissionBitOn( MODIFY_ANNOTATIONS_BIT ); 320 } 321 322 327 public void setCanModifyAnnotations( boolean allowAnnotationModification ) 328 { 329 setPermissionBit( MODIFY_ANNOTATIONS_BIT, allowAnnotationModification ); 330 } 331 332 337 public boolean canFillInForm() 338 { 339 return isPermissionBitOn( FILL_IN_FORM_BIT ); 340 } 341 342 347 public void setCanFillInForm( boolean allowFillingInForm ) 348 { 349 setPermissionBit( FILL_IN_FORM_BIT, allowFillingInForm ); 350 } 351 352 359 public boolean canExtractForAccessibility() 360 { 361 return isPermissionBitOn( EXTRACT_FOR_ACCESSIBILITY_BIT ); 362 } 363 364 370 public void setCanExtractForAccessibility( boolean allowExtraction ) 371 { 372 setPermissionBit( EXTRACT_FOR_ACCESSIBILITY_BIT, allowExtraction ); 373 } 374 375 381 public boolean canAssembleDocument() 382 { 383 return isPermissionBitOn( ASSEMBLE_DOCUMENT_BIT ); 384 } 385 386 391 public void setCanAssembleDocument( boolean allowAssembly ) 392 { 393 setPermissionBit( ASSEMBLE_DOCUMENT_BIT, allowAssembly ); 394 } 395 396 402 public boolean canPrintDegraded() 403 { 404 return isPermissionBitOn( DEGRADED_PRINT_BIT ); 405 } 406 407 413 public void setCanPrintDegraded( boolean allowAssembly ) 414 { 415 setPermissionBit( DEGRADED_PRINT_BIT, allowAssembly ); 416 } 417 } | Popular Tags |