Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a PDF is Password Protected or not

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();
}
like image 714
tusharagrawa Avatar asked Apr 09 '13 08:04

tusharagrawa


5 Answers

Here's a solution that doesn't require 3rd party libraries, using the PdfRenderer API.

 fun checkIfPdfIsPasswordProtected(uri: Uri, contentResolver: ContentResolver): Boolean {
    val parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r")
        ?: return false
    return try {
        PdfRenderer(parcelFileDescriptor)
        false
    } catch (securityException: SecurityException) {
        true
    }
}

Reference: https://developer.android.com/reference/android/graphics/pdf/PdfRenderer

like image 157
akodiakson Avatar answered Nov 15 '22 08:11

akodiakson


public boolean checkPdfEncrypted(InputStream fis) throws IOException {
    boolean encrypted = false;
    try {
        PDDocument doc = PDDocument.load(fis);
        if (doc.isEncrypted())
            encrypted = true;
        doc.close();
    } catch (
            IOException e) {
        encrypted = true;
    }
    return encrypted;
}

Note: There is one corner case in iText some file are encrypted protected but open without a password, to read those files and add water mark like this

PdfReader reader = new PdfReader(src);
reader.setUnethicalReading(true);
like image 30
Aasim ali Avatar answered Nov 15 '22 08:11

Aasim ali


In the old version of PDFBox

try
{
    InputStream fis = new ByteArrayInputStream(pdfBytes);                       
    PDDocument doc = PDDocument.load(fis);

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

In the newer version of PDFBox (e.g. 2.0.4)

    InputStream fis = new ByteArrayInputStream(pdfBytes);
    boolean encrypted = false;
    try {
        PDDocument doc = PDDocument.load(fis);
        if(doc.isEncrypted())
            encrypted=true;
        doc.close();
    }
    catch(InvalidPasswordException e) {
        encrypted = true;
    }
    return encrypted;
like image 21
VHS Avatar answered Nov 15 '22 06:11

VHS


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.
    }
}
like image 32
Shreyos Adikari Avatar answered Nov 15 '22 07:11

Shreyos Adikari


The way I do it is by attempting to read the PDF file using PdfReader without passing a password of course. If the file is password protected, a BadPasswordException will be thrown. This is using the iText library.

like image 37
Waleed Almadanat Avatar answered Nov 15 '22 06:11

Waleed Almadanat