The PdfWriter class provides the setEncryption() method to set the password protection on a pdf file.
Syntax:
public void setEncryption(byte[] userPassword, byte[] ownerPassword, int permissions, int encryptionType) throws DocumentException.
1. userPassword – It specify the user password.
2. ownerPassword – It specify the owner password.
3. permissions – It specify the user permissions.
4. encryptionType – It specify the type of encryption.
1. userPassword – It specify the user password.
2. ownerPassword – It specify the owner password.
3. permissions – It specify the user permissions.
4. encryptionType – It specify the type of encryption.
Note: We can provide multiple permission by ORing them.
e.g. PdfWriter. ALLOW_COPY | PdfWriter.ALLOW_PRINTING
Steps:
1. Create Document instance. It represents the current document to which we are adding content.
2. Create OutputStream instance. It represents the generated pdf.
3. Create PDFWriter instance and pass Document and OutputStream instance to its constructor.
4. Add password protection using setEncryption() method of the PdfWriter class.
5. Open the Document by calling document.open().
6. Add the content to the document by calling document.add() method.
7. Close the document by calling document.close() method.
2. Create OutputStream instance. It represents the generated pdf.
3. Create PDFWriter instance and pass Document and OutputStream instance to its constructor.
4. Add password protection using setEncryption() method of the PdfWriter class.
5. Open the Document by calling document.open().
6. Add the content to the document by calling document.add() method.
7. Close the document by calling document.close() method.
Example:
PDFPasswordExample.java
No comments:
Post a Comment