Remove encryption from PDF file using Apache PDFBox

19,464

This is what you'd need to do (inspired from the PDFBox WriteDecodedDoc command line tool):

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

Note: you may have to include the Bouncy Castle JAR.

Share:
19,464
Josh Nankin
Author by

Josh Nankin

Updated on July 07, 2022

Comments

  • Josh Nankin
    Josh Nankin almost 2 years

    With QPDF, you can simply remove restrictions / encryption from a PDF file 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 does this?

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

  • koppor
    koppor about 8 years
    This is the deprecated API. Please use the new API as outlined at stackoverflow.com/a/29676262/873282.
  • Enrico Giurin
    Enrico Giurin over 7 years
    there is not such a method doc.decrypt() - it's enough to use doc.setAllSecurityToBeRemoved(true);