Document is actually a PDDocument, so a more complete snippet would be:

for (File file : files) {
        PDDocument document = PDDocument.load(file.getAbsolutePath());
        if (document.isEncrypted()) {
                try {
                        document.decrypt("your password");
                        document.setAllSecurityToBeRemoved(true);
                } catch (InvalidPasswordException e) {
                        System.err.println("Error: Document is encrypted with a 
different password.");
                        System.exit(1);
                }
        else {
                System.err.println("Error: Document is not encrypted.");
        }
        document.close();
}

On 11. 2. 2012., at 01:27, Ilija Pavlic wrote:

> if (document.isEncrypted()) {
>                               try {
>                                       document.decrypt("your password");
>                               } catch (InvalidPasswordException e) {
>                                       System.err.println("Error: Document is 
> encrypted with a different password."); // so, now you know that your 
> password is incorrect for your file
>                                       System.exit(1);
>                               }
>                       }
> 
> BR,
> Ilija.
> 
> On 11. 2. 2012., at 01:22, [email protected] wrote:
> 
>> How can a java program check to see if a given password is the correct 
>> password to open a pdf document?  We have a table that shows what password 
>> should have been set as the correct password for a given file.  
>> 
>> I want to traverse this list of files and figure out which files had the 
>> password set correctly and which did not.  I can traverse the table, but 
>> what piece of code or what method or methods in PDFBOX will let me check if 
>> a particular password is the right document open password?
>> 
>> Thanks to anyone who can shed any light on this.
>> 
>> - Frank
> 

Reply via email to