How to protect an already existing PDF with a password?
How do I set a password for an existing PDF?
Posted on StackOverflow on Dec 2, 2014 by Sreekanth P
It's as simple as this, from EncryptionPdf:
public void encryptPdf(String src, String dest) throws IOException {
PdfWriter writer = new PdfWriter(dest, new WriterProperties()
.setStandardEncryption(USER, OWNER,
EncryptionConstants.ALLOW_PRINTING,
EncryptionConstants.ENCRYPTION_AES_128 | EncryptionConstants.DO_NOT_ENCRYPT_METADATA));
PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), writer);
pdfDoc.close();
writer.close();
}
Note that USER and OWNER are of type byte[]:
public static byte[] USER = "Hello".getBytes();
public static byte[] OWNER = "World".getBytes();
You have different options for the permissions (look for EncryptionConstants starting with ALLOW_) and also you can chose from different encryption algorithms.
Click How to protect an already existing PDF with a password? if you want to see how to answer this question in iText 5.