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 java.io.*;
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();
            filename="Hello.pdf";
            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);
            EncryptPDF.createXmpMetadata();
            Document_For_Protection.open();
            Document_For_Protection.add(new Paragraph("Some Contents for Password Protection"));
            Document_For_Protection.close();
        }
        catch (Exception i)
        {
            System.out.println(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>(PdfEncryption.java:138)
        at com.itextpdf.text.pdf.PdfWriter.setEncryption(PdfWriter.java:1992)
        at PasswordProtectPDF.main(image.java:16)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.ASN1OctetStri
ng
        at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
        ... 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

4 comments:

  1. Thanx.....
    very useful

    ReplyDelete
  2. Thanks, helped to solve my problem

    ReplyDelete
  3. Thank you.. Simply solved my Requirement.

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

    ReplyDelete