Nitin
Nitin

Reputation: 2911

PDFBox returns isEncrypted true even if i can open file

I am using PDFBox to determine pdf file is password protected or not. this is my code:

boolean isProtected = pdfDocument.isEncrypted();

My file properties is in sceenshot. Here i am getting isProtected= true even i can open it without password.

Note: this file has Document Open password : No and permission password : Yes.

click here to view file

Upvotes: 0

Views: 3224

Answers (2)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522824

I am posting this answer because elsewhere on Stack Overflow and the web you might see the suggested way to check for a password protected PDF in PDFBox is to use PDDocument#isEncrypted(). The problem we found with this is that certain PDFs which did not prompt for a password were still being flagged as encrypted. See the accepted answer for one explanation of why this is happening, but in any case we used the followed pattern as a workaround:

boolean isPDFReadable(byte[] fileContent) {
    PDDocument doc = null;
    try {
        doc = PDDocument.load(fileContent);
        doc.getPages();  // perhaps not necessary
        return true;
    }
    catch (InvalidPasswordException invalidPasswordException) {
        LOGGER.error("Unable to read password protected PDF.", invalidPasswordException);
    }
    catch (IOException io) {
        LOGGER.error("An error occurred while reading a PDF attachment during account submission.", io);
    }
    finally {
        if (!Objects.isNull(doc)) {
            try {
                doc.close();
                return true;
            }
            catch (IOException io) {
                LOGGER.error("An error occurred while closing a PDF attachment ", io);
            }
        }
    }

    return false;
}

If the call to PDDocument#getPages() succeeds, then it also should mean that opening the PDF via double click or browser, without a password, should be possible.

Upvotes: 1

Tilman Hausherr
Tilman Hausherr

Reputation: 18956

Your PDF has an empty user password and a non empty owner password. And yes, it is encrypted. This is being done to prevent people to do certain things, e.g. content copying.

It isn't a real security; it is the responsibility of the viewer software to take care that the "forbidden" operations aren't allowed.

You can find a longer (and a bit amusing) explanation here.

To see the document access permissions, use PDDocument.getCurrentAccessPermission().

In 2.0.*, a user will be able to view a file if this call succeeds:

PDDocument doc = PDDocument.load(file);

If a InvalidPasswordException is thrown, then it means a non empty password is required.

Upvotes: 6

Related Questions