How to check if a PDF is Password Protected or not

自作多情 提交于 2019-11-30 17:46:36

问题


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();
}

回答1:


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.
    }
}



回答2:


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.




回答3:


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;



回答4:


Try this code:

    boolean isProtected = true;
    PDDocument pdfDocument = null;
    try
    {
        pdfDocument = PDDocument.load(new File("your file path"));
        isProtected = false;

    }
    catch(Exception e){
        LOG.error("Error while loading file : ",e);
    }
    Syste.out.println(isProtected);

If your document is password protected then it can not load document and throw IOException. Verified above code using pdfbox-2.0.4.jar




回答5:


try {
    PdfReader pdfReader = new PdfReader(String.valueOf(file));
    pdfReader.isEncrypted();
} catch (IOException e) {
    e.printStackTrace();
}

Using iText PDF library you can check. If it went to an exception handle it(ask for password)




回答6:


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



来源:https://stackoverflow.com/questions/15896691/how-to-check-if-a-pdf-is-password-protected-or-not

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!