You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@pdfbox.apache.org by Roberto Nibali <rn...@gmail.com> on 2015/07/27 14:23:43 UTC

Setting PDF Document Restrictions and Security Limitations

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

The resulting restrictions and security settings are dead wrong, as seen in
these screenshots:

The original PDF:

​​​
 DocumentRestrictionsSummary-OriginalPDF.png
<https://drive.google.com/file/d/0B7Bzk_1dcyc5bTZueXlGbHV5RVU/view?usp=drive_web>
​​​​
 DocumentSecurity-OriginalPDF.png
<https://drive.google.com/file/d/0B7Bzk_1dcyc5R0VMWjU2a0toUnc/view?usp=drive_web>
​

The modified PDF:

​​​
 DocumentRestrictionsSummary-NewPDF.png
<https://drive.google.com/file/d/0B7Bzk_1dcyc5by00SzFLcmxHdFk/view?usp=drive_web>
​​
 DocumentSecurity-NewPDF.png
<https://drive.google.com/file/d/0B7Bzk_1dcyc5TUpqYzBTS0Mxa3c/view?usp=drive_web>
​

I have no idea where to look for in the PDFDebugger to figure out how to
set those document options using COS objects. Can anyone point me to the
respective sections please? Also, it seems as if the API in PDFBox does not
allow to set all aspects of those settings, or at least I couldn't find
them.

My basic question: How do I copy the existing document security settings
from the original document to the new one using PDFBox?

Best regards
Roberto

Re: Setting PDF Document Restrictions and Security Limitations

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 27.07.2015 um 18:50 schrieb Roberto Nibali:
> 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)

Heh heh, I included this warning. (Based on problems you had)

>
> About a month ago, I received the hint that I had to set this in order to
> save the PDF to a new file.
Yes it is needed if you want an unencrypted file, or if the file was 
encrypted before and not protected again.

Tilman


> For unknown reasons, I do not have to fiddle
> around with setAllSecurityToBeRemoved() anymore. I'm very glad;).


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Setting PDF Document Restrictions and Security Limitations

Posted by Roberto Nibali <rn...@gmail.com>.
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

Re: Setting PDF Document Restrictions and Security Limitations

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

> Am 27.07.2015 um 18:26 schrieb Roberto Nibali <rn...@gmail.com>:
> 
> Hi Maruan
> 
> On Mon, Jul 27, 2015 at 3:59 PM, Maruan Sahyoun <sa...@fileaffairs.de>
> wrote:
> 
>> Hi,
>> 
>>> Am 27.07.2015 um 14:45 schrieb Roberto Nibali <rn...@gmail.com>:
>>> 
>>> Hello
>>> 
>>> I've managed to somehow hardcode the security settings:
>>> 
>>> On Mon, Jul 27, 2015 at 2:23 PM, Roberto Nibali <rn...@gmail.com>
>> 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: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Setting PDF Document Restrictions and Security Limitations

Posted by Roberto Nibali <rn...@gmail.com>.
Hi Maruan

On Mon, Jul 27, 2015 at 3:59 PM, Maruan Sahyoun <sa...@fileaffairs.de>
wrote:

> Hi,
>
> > Am 27.07.2015 um 14:45 schrieb Roberto Nibali <rn...@gmail.com>:
> >
> > Hello
> >
> > I've managed to somehow hardcode the security settings:
> >
> > On Mon, Jul 27, 2015 at 2:23 PM, Roberto Nibali <rn...@gmail.com>
> 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

Re: Setting PDF Document Restrictions and Security Limitations

Posted by Maruan Sahyoun <sa...@fileaffairs.de>.
Hi,

> Am 27.07.2015 um 14:45 schrieb Roberto Nibali <rn...@gmail.com>:
> 
> Hello
> 
> I've managed to somehow hardcode the security settings:
> 
> On Mon, Jul 27, 2015 at 2:23 PM, Roberto Nibali <rn...@gmail.com> 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.

BR
Maruan



> Let's hope all documents I need to migrate have the same security settings.
> For now, this issue does not need to be looked at anymore. What's
> funny is that using PDFBox, I cannot modify the
> page extraction, signing or the creation of template pages bits in
> the security settings. Is this missing in the implementation?
> 
> Cheers
> 
> Roberto


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Re: Setting PDF Document Restrictions and Security Limitations

Posted by Roberto Nibali <rn...@gmail.com>.
Hello

I've managed to somehow hardcode the security settings:

On Mon, Jul 27, 2015 at 2:23 PM, Roberto Nibali <rn...@gmail.com> 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);
}

Let's hope all documents I need to migrate have the same security settings.
For now, this issue does not need to be looked at anymore. What's
funny is that using PDFBox, I cannot modify the
 page extraction, signing or the creation of template pages bits in
the security settings. Is this missing in the implementation?

Cheers

Roberto

Re: Setting PDF Document Restrictions and Security Limitations

Posted by Roberto Nibali <rn...@gmail.com>.
Hi Tilman

On Mon, Jul 27, 2015 at 5:36 PM, Tilman Hausherr <TH...@t-online.de>
wrote:

> Am 27.07.2015 um 14:23 schrieb Roberto Nibali:
>
>> 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);
>>
>
> This is because when opening a document with the owner password, you get
> all permissions. To get restricted permissions, open the document with the
> user password. (usually empty)
>
> Try it... (I haven't tested your files)
>
> Tilman
>
>
And indeed, this worked. The code looks like this now:

public static void SimpleTest() throws IOException {
    srcDoc = PDDocument.load(new File(srcDocName), "");
    tplDoc = PDDocument.load(new File(tplDocName), "");
    tplDoc.setAllSecurityToBeRemoved(true);
    applyDocPermissions(ownerPassword, "");
    srcDoc.close();
    tplDoc.getDocumentCatalog().getAcroForm().setNeedAppearances(true);
    tplDoc.save(outDocName);
    tplDoc.close();
}

Thanks. I will now have a look at Maruan's proposal on top of this.

Cheers
Roberto

Re: Setting PDF Document Restrictions and Security Limitations

Posted by Tilman Hausherr <TH...@t-online.de>.
Am 27.07.2015 um 14:23 schrieb Roberto Nibali:
> 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);

This is because when opening a document with the owner password, you get 
all permissions. To get restricted permissions, open the document with 
the user password. (usually empty)

Try it... (I haven't tested your files)

Tilman

>      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);
> }
>
> The resulting restrictions and security settings are dead wrong, as seen in
> these screenshots:
>
> The original PDF:
>
> ​​​
>   DocumentRestrictionsSummary-OriginalPDF.png
> <https://drive.google.com/file/d/0B7Bzk_1dcyc5bTZueXlGbHV5RVU/view?usp=drive_web>
> ​​​​
>   DocumentSecurity-OriginalPDF.png
> <https://drive.google.com/file/d/0B7Bzk_1dcyc5R0VMWjU2a0toUnc/view?usp=drive_web>
> ​
>
> The modified PDF:
>
> ​​​
>   DocumentRestrictionsSummary-NewPDF.png
> <https://drive.google.com/file/d/0B7Bzk_1dcyc5by00SzFLcmxHdFk/view?usp=drive_web>
> ​​
>   DocumentSecurity-NewPDF.png
> <https://drive.google.com/file/d/0B7Bzk_1dcyc5TUpqYzBTS0Mxa3c/view?usp=drive_web>
> ​
>
> I have no idea where to look for in the PDFDebugger to figure out how to
> set those document options using COS objects. Can anyone point me to the
> respective sections please? Also, it seems as if the API in PDFBox does not
> allow to set all aspects of those settings, or at least I couldn't find
> them.
>
> My basic question: How do I copy the existing document security settings
> from the original document to the new one using PDFBox?
>
> Best regards
> Roberto
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org