[ 
https://issues.apache.org/jira/browse/PDFBOX-807?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13541652#comment-13541652
 ] 

Karl Nicholas commented on PDFBOX-807:
--------------------------------------

I don't really think this should be marked as fix and it should be reopened. I 
ran across this bug in versino 1.7.1. I worked around the issue by calling 
PDDocument#encrypt("", "") before calling PDDocument#save(). I have to call 
this even though the PDDocument says it's not encryped. The document was once 
was encrypted and had to be decrypted after it was loaded. When you call 
PDDocument#save() it realizes that it once was, or should be, encrypted and 
improperly constructs StandardSecurityHandler without a 
StandardProtectionPolicy. StandardProtectionPolicy is where the passwords and 
Permissions are stored, so that's why it gets a NPE when trying to encrypt the 
document before "saving" it. 

I chased the problem a little bit. The issue is that the document was 
encrypted, so the I decrypt it after loading it, so as to work on it. In my 
case I was reading and writing fields. Then, when I call myPDFDoc.save, PDFBox 
realizes the document "wants" to be encrypted but the proper constructor in 
...StandardSecurityHandler is not called. Calling PDDocument#encrypt("", "") 
causes the proper StandardSecurityHandler constructror to be called and 
instantiated in the document.

True, you can also work around the problem by calling 
PDDocument#setAllSecurityToBeRemoved(true), but this merely makes the document 
get saved as unencrypted. This may not be what a PDFBox user wants.

Anyway, I think it points to a larger issue about how consistantly encryption 
is being handled in a API and implementation.
                
> NullPointerException in StandardSecurityHandler.java:261
> --------------------------------------------------------
>
>                 Key: PDFBOX-807
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-807
>             Project: PDFBox
>          Issue Type: Bug
>          Components: PDModel
>    Affects Versions: 1.2.1
>         Environment: JDK 1.6.0_21, Windows XP 32 Bit.
>            Reporter: MH
>            Assignee: Andreas Lehmkühler
>            Priority: Blocker
>             Fix For: 1.7.0
>
>         Attachments: enc_test_test_file.pdf
>
>
> I like to add text to a PDF and used the example code 
> AddMessageToEachPage.java to do this. I added decryption to support encrypted 
> docs:
>                 doc = PDDocument.load(sourceFile);
>                 if (doc.isEncrypted()) {
>                     doc.decrypt(ownerPwd);
>                 }
> When my document is encrypted, the decryption succeeds without error message, 
> but for such encrypted docs, when callinf doc.save(), a NullPointerException 
> occurs:
> -----------------------------------------
>     java.lang.NullPointerException
>       at 
> org.apache.pdfbox.pdmodel.encryption.StandardSecurityHandler.prepareDocumentForEncryption(StandardSecurityHandler.java:261)
>       at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1013)
>       at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:911)
>       at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:892)
> -----------------------------------------------------------------

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to