Hi,

> Am 27.07.2015 um 18:26 schrieb Roberto Nibali <[email protected]>:
> 
> 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);
> }

glad it worked. You should also be able to omit the line

   tplDoc.setAllSecurityToBeRemoved(false);

makes it even shorter :-)

> 
> Together with Tilman's change, this part of the code is now small and
> concise.

good to hear that.

BR
Maruan

> 
> Best regards
> Roberto


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to