List of usage examples for org.apache.pdfbox.pdmodel.encryption StandardProtectionPolicy setPermissions
public void setPermissions(AccessPermission permissions)
From source file:org.nuxeo.pdf.PDFEncryption.java
License:Apache License
/** * Encrypts the pdf with the new permissions (see {@link AccessPermission}) * <p>/*from w w w. ja v a 2s .c om*/ * <b>IMPORTANT * </p> * : It is required that the following setters are called <i>before</i> * <ul> * <li>{@link setOriginalOwnerPwd}: Only if the original pdf already is encrypted. This password allows to open it * for modification</li> * <li>{@link setKeyLength}: To set the length of the key</li> * <li>{@link setOwnerPwd}: The password for the owner. If not called, <code>originalOwnerPwd</code> is used instead * </li> * <li>{@link setUserPwd}: The password for the user.</li> * </ul> * * @param inPerm * @return a copy of the blob with the new permissions set. * @since 8.1 */ public Blob encrypt(AccessPermission inPerm) { Blob result = null; if (keyLength < 1) { keyLength = DEFAULT_KEYLENGTH; } else { if (!ALLOWED_LENGTH.contains(keyLength)) { throw new NuxeoException( "Cannot use " + keyLength + " is not allowed as lenght for the encrytion key"); } } if (StringUtils.isBlank(ownerPwd)) { ownerPwd = originalOwnerPwd; } try { loadPdf(); StandardProtectionPolicy spp = new StandardProtectionPolicy(ownerPwd, userPwd, inPerm); spp.setEncryptionKeyLength(keyLength); spp.setPermissions(inPerm); pdfDoc.protect(spp); result = PDFUtils.saveInTempFile(pdfDoc, pdfBlob.getFilename()); } catch (Exception e) { throw new NuxeoException("Failed to encrypt the PDF", e); } finally { PDFUtils.closeSilently(pdfDoc); pdfDoc = null; } return result; }