如何在不使用第三方解决方案的情况下检查任何PDF是否在Android中受密码保护



我正试图使用PdfRenderer(Android的内置解决方案(在我的应用程序中导入pdf。

有了这个,我面临着这个链接上提到的问题。

谷歌发布

我的要求是在不使用任何第三方libs或jar的情况下检查任何密码是否受密码保护。

如何做到这一点?

注意:类似的报告问题只讨论使用第三方libs的解决方案,但这不是我想要的。

我已经能够使用PdfRenderer类完成大部分工作了。只是为了检查任何pdf是否有密码保护,我不想使用任何库。

在Android SDK中,有Android.graphics.pdf包可以处理pdf文档,但没有合适的方法来检查pdf密码是否有效。

因此,我建议在不打开pdf文档的情况下使用PdfBox Android库来检查pdf密码是否有效。

这是它的示例代码。

public boolean isPdfPasswordValid(String filePath, String password) {
    try {
        File file = new File(filePath);
        PDDocument document = PDDocument.load(file,password);
         if (document.isEncrypted()) {
             document.setAllSecurityToBeRemoved(true);
             document.getPage(0);
             document.close();
             return true;
         } else {
             document.close();
             return false;
         }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

在android项目中的模块级build.gradle文件中添加此渐变依赖性

implementation 'com.tom-roush:pdfbox-android:2.0.27.0'

相关内容

最新更新