Java Password Protect PDF File

In this post, we will present a working example of how to protect a PDF document with a Password in Java.  To encrypt a PDF document with a password, we will use iText library and I would recommend downloading the latest version of iText before going any further. The code example to encrypt a PDF document is provided below
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class PasswordProtectPDF {    
    public static byte[] UserPassword= "UserPassword".getBytes();    
    public static byte[] OwnerPassword = "OwnerPassword".getBytes();
    public static void main(String[] args){
        try {
            String filename=new String();
            Document Document_For_Protection = new Document();
            PdfWriter EncryptPDF= PdfWriter.getInstance(Document_For_Protection, new FileOutputStream(filename));
            EncryptPDF.setEncryption(UserPassword, OwnerPassword,
            PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128);
            Document_For_Protection.add(new Paragraph("Some Contents for Password Protection"));
        catch (Exception i)
When you run this example,if you get an exception as shown below
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1OctetString
        at com.itextpdf.text.pdf.PdfEncryption.<init>(
        at com.itextpdf.text.pdf.PdfWriter.setEncryption(
        at PasswordProtectPDF.main(
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.ASN1OctetStri
        at Method)
        at java.lang.ClassLoader.loadClass(
        at sun.misc.Launcher$AppClassLoader.loadClass(
        at java.lang.ClassLoader.loadClass(
        ... 3 more
then this means you have not loaded the supporting JAR file bcprov-jdk15-136.jar which is required to achieve the encryption. You can download a copy of the JAR file from the web and set it in your classpath for the encryption example to work properly.

In this code example, we use the "setEncryption" method of PdfWriter class to protect the PDF file. This method takes four parameters:

1) User Password :- We have set this to "UserPassword" in our example.
2) Owner Password :- This is set to "OwnerPassword" in our example.
3) Permissions:- As per the iText documentation, the permissions can be any one or a combination of the following: Printing, Modify Contents, Copying, Modify Annotations,Fill In, Allow Screen Readers, Allow Assembly and Allow Degraded Printing. In our code example, we have given ALLOW_PRINTING, which means, when you successfully open the document with user password, you will be able to print the document. But, you will not be able to modify the contents and so on..
4) Encryption Strength: This can be of STANDARD_ENCRYPTION_40, STANDARD_ENCRYPTION_128 or ENCRYPTION_AES128

When you run the example, you will get a PDF document "Hello.pdf", which will prompt you for a password for open. If you attempt to open the document before doing the encryption call, iText will throw the following exception at runtime; (DocumentException)
com.itextpdf.text.DocumentException: Encryption can only be added before opening the document.
The document security for the PDF file created by the java code is shown in the screenshot below

PDF Security Options - iText Password Protection


  1. Thanx.....
    very useful

  2. Thanks, helped to solve my problem

  3. Thank you.. Simply solved my Requirement.

  4. Even after adding bcprov-jdk15-136.jar to my library exception is coming....i am using NetBean IDE....Thanx in advanced