remove encryption from pdf with pdfbox, like qpdf

送分小仙女□ 提交于 2019-11-30 04:35:07

问题


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

qpdf --decrypt infile outfile

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

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?

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


回答1:


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)

    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);
        }
    }


来源:https://stackoverflow.com/questions/14700241/remove-encryption-from-pdf-with-pdfbox-like-qpdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!