Thursday, June 21, 2018

Protect PDF using password in iText

When you need to protect PDF files using password, you can use following iText codes to encrypt PDF using passwords.

String ownerPasswd="owner";
String userPasswd="user";

PdfReader reader = new PdfReader("/apps/abc.pdf");
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("/apps/abc_pw.pdf"));
stamper.setEncryption(userPasswd.getBytes(), ownerPasswd.getBytes(),
PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128 | PdfWriter.DO_NOT_ENCRYPT_METADATA);
stamper.close();
reader.close();

Library versions used,
itextpdf-5.5.10.jar
bcprov-ext-jdk14-1.47.jar

You can use preferred encryption algorithms. Refer documentations.

No comments:

Post a Comment