remove encryption from pdf with pdfbox, like qpdf

Josh Nankin picture Josh Nankin · Feb 5, 2013 · Viewed 16.5k times · Source

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

Answer

Josh Nankin picture Josh Nankin · Feb 5, 2013

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