pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Roberto Nibali <rnib...@gmail.com>
Subject Re: Setting PDF Document Restrictions and Security Limitations
Date Mon, 27 Jul 2015 16:50:22 GMT
Hi Maruan


>>>> @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);
>

For some reason, in the past I had to set this; which is the reason why I
further down had the call to set it false again. Removing this and your
suggestion still works, so the code finally looks very clean and ledgible
with regard to what one would expect.


> >>>>   applyDocPermissions(ownerPassword, "");
> >>>>   srcDoc.close();
> >>>>   tplDoc.getDocumentCatalog().getAcroForm().setNeedAppearances(true);
> >>>>   tplDoc.save("ccmig.pdf");
> >>>>   tplDoc.close();
> >>>> }
> >> 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 :-)


Indeed, however only if the above counter-part is also removed. Otherwise,
we get a warning:

Jul 27, 2015 6:49:42 PM org.apache.pdfbox.pdmodel.PDDocument protect
WARNING: do not call setAllSecurityToBeRemoved(true) before calling
protect(), as protect() implies setAllSecurityToBeRemoved(false)

About a month ago, I received the hint that I had to set this in order to
save the PDF to a new file. For unknown reasons, I do not have to fiddle
around with setAllSecurityToBeRemoved() anymore. I'm very glad ;).

Cheers

Roberto

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message