How to protect an already existing PDF with a password?

How to set password for an existing PDF?

30th October 2015
admin-marketing

Posted on StackOverflow on Dec 2, 2014 by Sreekanth P

It's as simple as this:

public void encryptPdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.setEncryption(USER, OWNER, PdfWriter.ALLOW_PRINTING,
        PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
    stamper.close();
    reader.close();
}

Note that USER and OWNER are of type byte[]. You have different options for the permissions (look for constants starting with ALLOW_) and you can choose from different encryption algorithms.



Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now