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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14700241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:05:33  来源:igfitidea点击:

remove encryption from pdf with pdfbox, like qpdf

javapdfpdfbox

提问by Josh Nankin

With qpdf, you can simply remove restrictions/encryption from a pdf like so:

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

qpdf --decrypt infile outfile

I would like to do the same thing with PDFBox in Java:

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

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

I've tried this with StandardDecryptionMaterial, but I have no idea what the owner password is. How does qpdf do this?

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

Sample document: https://issues.apache.org/jira/secure/attachment/12514714/in.pdf

示例文档:https: //issues.apache.org/jira/secure/attachment/12514714/in.pdf

回答by Josh Nankin

This is what you'd need to do. Inspired from the PDFBox WriteDecodedDoc tool. You may have to include the bouncycastle jar (http://www.bouncycastle.org/latest_releases.html)

这就是你需要做的。灵感来自 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);
        }
    }