【问题标题】:PDFBox not recognising pdf to be non printablePDFBox 无法识别 pdf 不可打印
【发布时间】:2013-11-21 20:26:11
【问题描述】:

我正在使用 PDFBox 验证 pdf 文档,其中一项验证说明 pdf 文档是否可打印。

我使用下面的代码来执行这个操作:

PDDocument document = PDDocument.load("<path_to_pdf_file>");
System.out.println(document.getCurrentAccessPermission().canPrint());

但是当打开 pdf 时,它显示打印图标已禁用,这让我返回 true。

【问题讨论】:

  • 请提供有问题的 pdf。否则这将是纯粹的猜测。
  • 星期一我会在办公室看文件。

标签: pdf printing pdfbox detection


【解决方案1】:

访问权限通过加密方式集成到文档中。

即使是在 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");
}

【讨论】:

  • 谢谢。解决方案有效,但正如您所说,如果密码不为空,那么我们就有问题了。根据我的要求解密此类文件的任何建议是接收 pdf 文件并对其运行验证。
  • @Krishnendu 我在我的答案中添加了一种不需要文件解密的替代方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-08
  • 2013-09-09
  • 2016-06-15
  • 1970-01-01
相关资源
最近更新 更多