views:

54

answers:

2

Hello,

I need to encrypt and decrypt pdf files. Is there a free or low cost Java API that does that ? Basically I need to hide files from normal users. Any other suggestion on achieving that programatically ?

Thanks, Deep

+4  A: 

iText supports encryption.

Bill the Lizard
+1  A: 

Using iText:

  // Assuming you provide the following yourself:
  File inputFile; 
  File outputFile;
  String userPassword;
  String ownerPassword;
  // A bit-field containing file permissions.
  int permissions = PDFWriter.ALLOW_PRINTING | PDFWriter.ALLOW_COPY;

  PdfReader reader = new PdfReader(inputFile);
  PdfEncryptor.encrypt(reader, new FileOutputStream(outputFile),
      ENCRYPTION_AES128, userPassword, ownerPassword, 
      permissions);

Here's the API for PDFEncryptor and PDFWriter (for the permissions).

Frederik
Thanks Frederik. Can you also provide code to decrypt the encrypted file ?
DG