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

时间:2013-02-05 04:29:06

标签: java pdf pdfbox

使用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

1 个答案:

答案 0 :(得分: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);
        }
    }