我正在使用PDFBox來驗證PDF文檔,並確認PDF文檔是否可打印。PDFBox不承認PDF是不可打印的
我用下面的代碼來執行此操作:
PDDocument document = PDDocument.load("<path_to_pdf_file>");
System.out.println(document.getCurrentAccessPermission().canPrint());
但這回我真雖然當PDF被打開,它顯示了禁用的打印圖標。
我正在使用PDFBox來驗證PDF文檔,並確認PDF文檔是否可打印。PDFBox不承認PDF是不可打印的
我用下面的代碼來執行此操作:
PDDocument document = PDDocument.load("<path_to_pdf_file>");
System.out.println(document.getCurrentAccessPermission().canPrint());
但這回我真雖然當PDF被打開,它顯示了禁用的打印圖標。
通過加密將訪問權限集成到文檔中。
甚至在Acrobat Reader中打開時不要求密碼的PDF文檔也可能是加密的,它們本質上是使用默認密碼加密的。您的PDF中就是這種情況。
PDFBox僅在解密時確定加密PDF的權限,而不是在加載PDDocument
時才具有權限。因此,如果文件加密,則必須在檢查其屬性之前嘗試解密文件。
你的情況:
PDDocument document = PDDocument.load("<path_to_pdf_file>");
if (document.isEncrypted())
{
document.decrypt("");
}
System.out.println(document.getCurrentAccessPermission().canPrint());
空字符串""
表示默認密碼。如果使用不同的密碼對文件進行加密,則會在此處發生異常。因此,相應地抓住。
PS:如果你不知道問題的所有密碼,你仍然可以使用PDFBox的檢查的權限,但你必須工作更低水平:
PDDocument document = PDDocument.load("<path_to_pdf_file>");
if (document.isEncrypted())
{
final int PRINT_BIT = 3;
PDEncryptionDictionary encryptionDictionary = document.getEncryptionDictionary();
int perms = encryptionDictionary.getPermissions();
boolean printAllowed = (perms & (1 << (PRINT_BIT-1))) != 0;
System.out.println("Document encrypted; printing allowed?" + printAllowed);
}
else
{
System.out.println("Document not encrypted; printing allowed? true");
}
Thanks.The解決方案的工作原理,但正如你所說,如果密碼不是空白,那麼我們有一個問題。根據我的要求,解密這些文件的任何建議是接收pdf文件並在其上運行驗證。 – Krishnendu
@Krishnendu我添加了一個替代方法來解決我的問題,它不需要文件解密。 – mkl
請提供PDF格式的題。否則,這將是純粹的猜測。 – mkl
我會在辦公室週一看文件。 – mkl