使用pdfbox从pdf中删除加密,如qpdf

使用qpdf,您可以简单地从pdf中删除限制/加密,如下所示:

qpdf --decrypt infile outfile 

我想用Java中的PDFBox做同样的事情:

 PDDocument doc = PDDocument.load(inputFilename); if( doc.isEncrypted() ) { //remove the encryption to alter the document } 

我已经尝试使用StandardDecryptionMaterial,但我不知道所有者密码是什么。 qpdf如何做到这一点?

样本文件: https : //issues.apache.org/jira/secure/attachment/12514714/in.pdf

这是你需要做的。 灵感来自PDFBox WriteDecodedDoc工具。 您可能必须包含bouncycastle jar( http://www.bouncycastle.org/latest_releases.html

  if (doc.isEncrypted()) { try { doc.decrypt(""); doc.setAllSecurityToBeRemoved(true); } catch (Exception e) { throw new Exception("The document is encrypted, and we can't decrypt it.", e); } }