PDFBox returns isEncrypted true even if i can open file

↘锁芯ラ 提交于 2019-12-25 07:26:45

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/39571878/pdfbox-returns-isencrypted-true-even-if-i-can-open-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!