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

Java 使用pdfbox从pdf中删除加密,如qpdf,java,pdf,pdfbox,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,您只需从pdf中删除限制/加密,如下所示:

qpdf --decrypt infile outfile
我想用Java中的PDFBox做同样的事情:

PDDocument doc = PDDocument.load(inputFilename);
if( doc.isEncrypted() )
{
   //remove the encryption to alter the document
}
我用StandardDecryptionMaterial尝试过,但我不知道所有者密码是什么。qpdf是如何做到这一点的


示例文档:

这是您需要做的。灵感来源于PDFBox WriteCodedDoc工具。您可能需要包括bouncycastle jar()


根据PDF的加密类型,解密很容易(否则如何显示)。但是,它超越了这种加密的概念,允许任何未经授权(所有者密码)的人删除加密。如果您有加密的PDF文件,但没有所有者密码,则如果您需要未加密的版本,则需要求助于文档所有者。这是不推荐使用的API。请使用中概述的新API。没有这样的方法doc.decrypt()-使用doc.setAllSecurityToBeRemoved(true)就足够了;
    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);
        }
    }