Hi Maruan On Mon, Jul 27, 2015 at 3:59 PM, Maruan Sahyoun <[email protected]> wrote:
> Hi, > > > Am 27.07.2015 um 14:45 schrieb Roberto Nibali <[email protected]>: > > > > Hello > > > > I've managed to somehow hardcode the security settings: > > > > On Mon, Jul 27, 2015 at 2:23 PM, Roberto Nibali <[email protected]> > wrote: > > > >> Hi > >> > >> I'm trying to replicate the document security settings using PDFBox and > >> can't seem to get it working at all. This is the code I use: > >> > >> private static PDDocument srcDoc; > >> private static PDDocument tplDoc; > >> > >> @Test > >> public static void SimpleTest() throws IOException { > >> String ownerPassword = "12345"; > >> srcDoc = PDDocument.load(new File("./ccalt.pdf"), ownerPassword); > >> tplDoc = PDDocument.load(new File("./cctemp.pdf"), ownerPassword); > >> tplDoc.setAllSecurityToBeRemoved(true); > >> applyDocPermissions(ownerPassword, ""); > >> srcDoc.close(); > >> tplDoc.getDocumentCatalog().getAcroForm().setNeedAppearances(true); > >> tplDoc.save("ccmig.pdf"); > >> tplDoc.close(); > >> } > >> public static void applyDocPermissions(String ownerPassword, String > userPassword) throws IOException { > >> int encKeyLen; > >> AccessPermission srcDocAP = srcDoc.getCurrentAccessPermission(); > >> AccessPermission destDocAP = new AccessPermission(); > >> destDocAP.setCanAssembleDocument(srcDocAP.canAssembleDocument()); > >> destDocAP.setCanExtractContent(srcDocAP.canExtractContent()); > >> > destDocAP.setCanExtractForAccessibility(srcDocAP.canExtractForAccessibility()); > >> destDocAP.setCanFillInForm(srcDocAP.canFillInForm()); > >> destDocAP.setCanModify(srcDocAP.canModify()); > >> destDocAP.setCanModifyAnnotations(srcDocAP.canModifyAnnotations()); > >> destDocAP.setCanPrint(srcDocAP.canPrint()); > >> destDocAP.setCanPrintDegraded(srcDocAP.canPrintDegraded()); > >> StandardProtectionPolicy policy = new > StandardProtectionPolicy(ownerPassword, userPassword, destDocAP); > >> encKeyLen = srcDoc.getEncryption().getLength(); > >> policy.setEncryptionKeyLength(encKeyLen); > >> tplDoc.setAllSecurityToBeRemoved(false); > >> tplDoc.protect(policy); > >> } > >> > >> > > Using the following function, it works: > > > > public static void applyDocPermissionsHardcoded(String ownerPassword, > > String userPassword) throws IOException { > > int encKeyLen; > > > > AccessPermission srcDocAP = srcDoc.getCurrentAccessPermission(); > > AccessPermission destDocAP = new AccessPermission(); > > destDocAP.setCanPrint(true); > > //destDocAP.setCanPrintDegraded(false); > > destDocAP.setCanModify(false); > > destDocAP.setCanAssembleDocument(false); > > destDocAP.setCanExtractContent(false); > > destDocAP.setCanExtractForAccessibility(true); > > //Page Extraction = false > > destDocAP.setCanModifyAnnotations(false); > > destDocAP.setCanFillInForm(true); > > //Signing = true > > //Creation of Template Pages = true > > StandardProtectionPolicy policy = new > > StandardProtectionPolicy(ownerPassword, userPassword, destDocAP); > > encKeyLen = srcDoc.getEncryption().getLength(); > > policy.setEncryptionKeyLength(encKeyLen); > > tplDoc.setAllSecurityToBeRemoved(false); > > tplDoc.protect(policy); > > } > > > > can you try > > > StandardProtectionPolicy policy = new > StandardProtectionPolicy(ownerPassword, userPassword, null); > int encKeyLen = srcDoc.getEncryption().getLength(); > policy.setEncryptionKeyLength(encKeyLen); > policy.setPermissions(srcDocAP); > > > Please verify the settings in the details view of the Acrobat security tab. > > I have just tried it and it works! Thanks, it makes a lot of sense now: public static void applyDocPermissions(String ownerPassword, String userPassword) throws IOException { int encKeyLen; AccessPermission srcDocAP = srcDoc.getCurrentAccessPermission(); StandardProtectionPolicy policy = new StandardProtectionPolicy(ownerPassword, userPassword, null); encKeyLen = srcDoc.getEncryption().getLength(); policy.setEncryptionKeyLength(encKeyLen); policy.setPermissions(srcDocAP); tplDoc.setAllSecurityToBeRemoved(false); tplDoc.protect(policy); } Together with Tilman's change, this part of the code is now small and concise. Best regards Roberto

