2
votes

I want to encrypt my PDF but there seems to be an error. I am using iText and Eclipse.

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.exceptions.InvalidPdfException;

public class EncryptionPdf {

    public static void main(String[] args) throws IOException, DocumentException 
    {
        PdfReader reader = new PdfReader("C:/Users/Binaday/Desktop/PDF RESULTS/Booking Form.pdf");
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("C:/Users/Binaday/Desktop/PDF RESULTS/Booking Form2.pdf"));
        stamper.setEncryption("reader_password".getBytes(), "permission_password".getBytes(),   ~(PdfWriter.ALLOW_COPY | PdfWriter.ALLOW_PRINTING ), PdfWriter.STANDARD_ENCRYPTION_128);
        stamper.close();
    }
}

Here's the error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1Encodable
at com.itextpdf.text.pdf.PdfEncryption.(PdfEncryption.java:149)
at com.itextpdf.text.pdf.PdfWriter.setEncryption(PdfWriter.java:2119)
at EncryptionPdf.main(EncryptionPdf.java:16)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.ASN1Encodable
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more

1
Have you added the appropriate BouncyCastle jars to your project class path? - mkl
No, because I don't know that. Can you link me to the site where I can download? Thank you. - wizclark99
@Bruno already answered your request. If you wonder what these POM files are all about, iText (and many other projects nowadays) use the Apache build system maven. The POM files are the maven project definition files which among other things contain information on dependencies on other libraries. - mkl
@Jamal The exception was way better readable when it was still formatted as code. - mkl

1 Answers

3
votes

When you look at the POM file for iText, you see the following dependencies:

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.49</version>
    <type>jar</type>
    <scope>compile</scope>
    <optional>true</optional>
</dependency>
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcpkix-jdk15on</artifactId>
    <version>1.49</version>
    <type>jar</type>
    <scope>compile</scope>
    <optional>true</optional>
</dependency>

This means that you need the bcprov and the bcpkix jars version 1.49 from Bouncycastle: http://bouncycastle.org/java.html

If you are not using iText 5.5.0, please check the POM file as older version of iText might require older versions of BouncyCastle.