
Here are the encryption type options you can use. The last parameter is for the encryption type. PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING PDF Encryption You can optionally compose multiple permissions using the bitwise ORing operator. Otherwise, you can use any of the following permissions. When 0 is passed as a parameter, the user is only allowed to view the document. In the third argument of the setEncryption() method, we specify the allowed permissions.
When you don’t specify a user password, all users will be able to open the document without being prompted for a password, but the permissions and restrictions – if any – will remain in place. If the user opens the document with the owner password, he’s able to change the permissions. In this case, the user is able to print and copy the page. When a user opens this document with the user password, he’s only able to perform the actions, specified in the permissions parameter. PdfWriter.ENCRYPTION_AES_256 | PdfWriter.DO_NOT_ENCRYPT_METADATA) PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING, PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("password-protected.pdf")) We explain which permissions and encryptions you can use in the following section. Finally, the last argument is used to set the encryption type. The third argument sets the permissions for the PDF document. You need this password in order to change the content or permissions of this document. Owner Password: also referred as the “ permissions password“. This password is typically shared with the users who may read the PDF document. User Password: also referred as the “ open password“. The first and second arguments are the user and owner passwords, respectively.
In this example we use password encryption.
Īfter we created a PdfWriter we can optionally set an encryption. The bouncycastle dependency is used for digital encryption support. Add the following dependencies to your pom.xml and Maven will manage these dependencies automatically. We use Apache Maven to manage our project dependencies.