That is more a stackoverflow question because you would need to write some Java to do this. See for instance these posts:
https://stackoverflow.com/questions/3676004/java-api-for-encrypting-decrypting-pdf-files
https://stackoverflow.com/questions/13271184/how-can-i-encrypt-a-pdf-using-java
https://stackoverflow.com/questions/17019239/how-to-encrypt-a-existing-pdf-file-in-java
Regards,
Ronald