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

Jos*_*kin 7 java pdf pdfbox

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

qpdf --decrypt infile outfile
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

Jos*_*kin 18

这是你需要做的.灵感来自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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

  • 没有这样的方法 doc.decrypt() - 使用 doc.setAllSecurityToBeRemoved(true) 就足够了; (5认同)