How to check if a PDF is Password Protected or not

tusharagrawa picture tusharagrawa · Apr 9, 2013 · Viewed 18.7k times · Source

I am trying to use iText's PdfReader to check if a given PDF file is password protected or not, but am getting this exception:

Exception in thread "Main Thread" java.lang.NoClassDefFoundError:org/bouncycastle/asn1/ASN1OctetString

But when testing the same code against a non-password protected file it runs fine. Here is the complete code:

try {
    PdfReader pdf = new PdfReader("C:\\abc.pdf");
} catch (IOException e) {
    e.printStackTrace();
}

Answer

Shreyos Adikari picture Shreyos Adikari · Apr 9, 2013

Use Apache PDFBox - Java PDF Library from here:
Sample Code:

try
{
    document = PDDocument.load( "C:\\abc.pdf");

    if(document.isEncrypted())
    {
      //Then the pdf file is encrypeted.
    }
}