pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: Setting PDF Document Restrictions and Security Limitations
Date Mon, 27 Jul 2015 15:36:08 GMT
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


Mime
View raw message