1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 /* $Id: PDFEncryptionParams.java 1484190 2013-05-18 22:25:52Z lbernardo $ */ 19 20 package org.apache.fop.pdf; 21 22 /** 23 * This class holds the parameters for PDF encryption. 24 */ 25 public class PDFEncryptionParams { 26 27 private String userPassword = ""; //May not be null 28 private String ownerPassword = ""; //May not be null 29 30 private boolean allowPrint = true; 31 private boolean allowCopyContent = true; 32 private boolean allowEditContent = true; 33 private boolean allowEditAnnotations = true; 34 private boolean allowFillInForms = true; 35 private boolean allowAccessContent = true; 36 private boolean allowAssembleDocument = true; 37 private boolean allowPrintHq = true; 38 private boolean encryptMetadata = true; 39 40 private int encryptionLengthInBits = 128; 41 42 /** 43 * Creates a new instance. 44 * @param userPassword the user password 45 * @param ownerPassword the owner password 46 * @param allowPrint true if printing is allowed 47 * @param allowCopyContent true if copying content is allowed 48 * @param allowEditContent true if editing content is allowed 49 * @param allowEditAnnotations true if editing annotations is allowed 50 */ PDFEncryptionParams(String userPassword, String ownerPassword, boolean allowPrint, boolean allowCopyContent, boolean allowEditContent, boolean allowEditAnnotations, boolean encryptMetadata)51 public PDFEncryptionParams(String userPassword, String ownerPassword, 52 boolean allowPrint, 53 boolean allowCopyContent, 54 boolean allowEditContent, 55 boolean allowEditAnnotations, 56 boolean encryptMetadata) { 57 setUserPassword(userPassword); 58 setOwnerPassword(ownerPassword); 59 setAllowPrint(allowPrint); 60 setAllowCopyContent(allowCopyContent); 61 setAllowEditContent(allowEditContent); 62 setAllowEditAnnotations(allowEditAnnotations); 63 this.encryptMetadata = encryptMetadata; 64 } 65 66 /** 67 * Default constructor initializing to default values. 68 */ PDFEncryptionParams()69 public PDFEncryptionParams() { 70 //nop 71 } 72 73 /** 74 * Creates a copy of the given encryption parameters. 75 * 76 * @param source source encryption parameters 77 */ PDFEncryptionParams(PDFEncryptionParams source)78 public PDFEncryptionParams(PDFEncryptionParams source) { 79 setUserPassword(source.getUserPassword()); 80 setOwnerPassword(source.getOwnerPassword()); 81 setAllowPrint(source.isAllowPrint()); 82 setAllowCopyContent(source.isAllowCopyContent()); 83 setAllowEditContent(source.isAllowEditContent()); 84 setAllowEditAnnotations(source.isAllowEditAnnotations()); 85 setAllowAssembleDocument(source.isAllowAssembleDocument()); 86 setAllowAccessContent(source.isAllowAccessContent()); 87 setAllowFillInForms(source.isAllowFillInForms()); 88 setAllowPrintHq(source.isAllowPrintHq()); 89 setEncryptionLengthInBits(source.getEncryptionLengthInBits()); 90 encryptMetadata = source.encryptMetadata(); 91 } 92 93 /** 94 * Indicates whether copying content is allowed. 95 * @return true if copying is allowed 96 */ isAllowCopyContent()97 public boolean isAllowCopyContent() { 98 return allowCopyContent; 99 } 100 101 /** 102 * Indicates whether editing annotations is allowed. 103 * @return true is editing annotations is allowed 104 */ isAllowEditAnnotations()105 public boolean isAllowEditAnnotations() { 106 return allowEditAnnotations; 107 } 108 109 /** 110 * Indicates whether editing content is allowed. 111 * @return true if editing content is allowed 112 */ isAllowEditContent()113 public boolean isAllowEditContent() { 114 return allowEditContent; 115 } 116 117 /** 118 * Indicates whether printing is allowed. 119 * @return true if printing is allowed 120 */ isAllowPrint()121 public boolean isAllowPrint() { 122 return allowPrint; 123 } 124 125 /** 126 * Indicates whether revision 3 filling in forms is allowed. 127 * @return true if revision 3 filling in forms is allowed 128 */ isAllowFillInForms()129 public boolean isAllowFillInForms() { 130 return allowFillInForms; 131 } 132 133 /** 134 * Indicates whether revision 3 extracting text and graphics is allowed. 135 * @return true if revision 3 extracting text and graphics is allowed 136 */ isAllowAccessContent()137 public boolean isAllowAccessContent() { 138 return allowAccessContent; 139 } 140 141 /** 142 * Indicates whether revision 3 assembling document is allowed. 143 * @return true if revision 3 assembling document is allowed 144 */ isAllowAssembleDocument()145 public boolean isAllowAssembleDocument() { 146 return allowAssembleDocument; 147 } 148 149 /** 150 * Indicates whether revision 3 printing to high quality is allowed. 151 * @return true if revision 3 printing to high quality is allowed 152 */ isAllowPrintHq()153 public boolean isAllowPrintHq() { 154 return allowPrintHq; 155 } 156 157 /** 158 * Indicates whether Metadata should be encrypted. 159 * @return true or false 160 */ encryptMetadata()161 public boolean encryptMetadata() { 162 return encryptMetadata; 163 } 164 165 /** 166 * Returns the owner password. 167 * @return the owner password, an empty string if no password applies 168 */ getOwnerPassword()169 public String getOwnerPassword() { 170 return ownerPassword; 171 } 172 173 /** 174 * Returns the user password. 175 * @return the user password, an empty string if no password applies 176 */ getUserPassword()177 public String getUserPassword() { 178 return userPassword; 179 } 180 181 /** 182 * Sets the permission for copying content. 183 * @param allowCopyContent true if copying content is allowed 184 */ setAllowCopyContent(boolean allowCopyContent)185 public void setAllowCopyContent(boolean allowCopyContent) { 186 this.allowCopyContent = allowCopyContent; 187 } 188 189 /** 190 * Sets the permission for editing annotations. 191 * @param allowEditAnnotations true if editing annotations is allowed 192 */ setAllowEditAnnotations(boolean allowEditAnnotations)193 public void setAllowEditAnnotations(boolean allowEditAnnotations) { 194 this.allowEditAnnotations = allowEditAnnotations; 195 } 196 197 /** 198 * Sets the permission for editing content. 199 * @param allowEditContent true if editing annotations is allowed 200 */ setAllowEditContent(boolean allowEditContent)201 public void setAllowEditContent(boolean allowEditContent) { 202 this.allowEditContent = allowEditContent; 203 } 204 205 /** 206 * Sets the permission for printing. 207 * @param allowPrint true if printing is allowed 208 */ setAllowPrint(boolean allowPrint)209 public void setAllowPrint(boolean allowPrint) { 210 this.allowPrint = allowPrint; 211 } 212 213 /** 214 * Sets whether revision 3 filling in forms is allowed. 215 * @param allowFillInForms true if revision 3 filling in forms is allowed. 216 */ setAllowFillInForms(boolean allowFillInForms)217 public void setAllowFillInForms(boolean allowFillInForms) { 218 this.allowFillInForms = allowFillInForms; 219 } 220 221 /** 222 * Sets whether revision 3 extracting text and graphics is allowed. 223 * @param allowAccessContent true if revision 3 extracting text and graphics is allowed 224 */ setAllowAccessContent(boolean allowAccessContent)225 public void setAllowAccessContent(boolean allowAccessContent) { 226 this.allowAccessContent = allowAccessContent; 227 } 228 229 /** 230 * Sets whether revision 3 assembling document is allowed. 231 * @param allowAssembleDocument true if revision 3 assembling document is allowed 232 */ setAllowAssembleDocument(boolean allowAssembleDocument)233 public void setAllowAssembleDocument(boolean allowAssembleDocument) { 234 this.allowAssembleDocument = allowAssembleDocument; 235 } 236 237 /** 238 * Sets whether revision 3 printing to high quality is allowed. 239 * @param allowPrintHq true if revision 3 printing to high quality is allowed 240 */ setAllowPrintHq(boolean allowPrintHq)241 public void setAllowPrintHq(boolean allowPrintHq) { 242 this.allowPrintHq = allowPrintHq; 243 } 244 245 /** 246 * Whether the Metadata should be encrypted or not; default is true; 247 * @param encryptMetadata true or false 248 */ setEncryptMetadata(boolean encryptMetadata)249 public void setEncryptMetadata(boolean encryptMetadata) { 250 this.encryptMetadata = encryptMetadata; 251 } 252 253 /** 254 * Sets the owner password. 255 * @param ownerPassword The owner password to set, null or an empty String 256 * if no password is applicable 257 */ setOwnerPassword(String ownerPassword)258 public void setOwnerPassword(String ownerPassword) { 259 if (ownerPassword == null) { 260 this.ownerPassword = ""; 261 } else { 262 this.ownerPassword = ownerPassword; 263 } 264 } 265 266 /** 267 * Sets the user password. 268 * @param userPassword The user password to set, null or an empty String 269 * if no password is applicable 270 */ setUserPassword(String userPassword)271 public void setUserPassword(String userPassword) { 272 if (userPassword == null) { 273 this.userPassword = ""; 274 } else { 275 this.userPassword = userPassword; 276 } 277 } 278 279 /** 280 * Returns the encryption length. 281 * @return the encryption length 282 */ getEncryptionLengthInBits()283 public int getEncryptionLengthInBits() { 284 return encryptionLengthInBits; 285 } 286 287 /** 288 * Sets the encryption length. 289 * 290 * @param encryptionLength the encryption length 291 */ setEncryptionLengthInBits(int encryptionLength)292 public void setEncryptionLengthInBits(int encryptionLength) { 293 this.encryptionLengthInBits = encryptionLength; 294 } 295 toString()296 public String toString() { 297 return "userPassword = " + userPassword + "\n" 298 + "ownerPassword = " + ownerPassword + "\n" 299 + "allowPrint = " + allowPrint + "\n" 300 + "allowCopyContent = " + allowCopyContent + "\n" 301 + "allowEditContent = " + allowEditContent + "\n" 302 + "allowEditAnnotations = " + allowEditAnnotations + "\n" 303 + "allowFillInForms = " + allowFillInForms + "\n" 304 + "allowAccessContent = " + allowAccessContent + "\n" 305 + "allowAssembleDocument = " + allowAssembleDocument + "\n" 306 + "allowPrintHq = " + allowPrintHq + "\n" 307 + "encryptMetadata = " + encryptMetadata; 308 } 309 310 } 311