簡體   English   中英

如何檢查 PDF 是否受密碼保護

[英]How to check if a PDF is Password Protected or not

我正在嘗試使用 iText 的 PdfReader 來檢查給定的 PDF 文件是否受密碼保護,但出現此異常:

線程“主線程”中的異常 java.lang.NoClassDefFoundError:org/bouncycastle/asn1/ASN1OctetString

但是當針對非密碼保護的文件測試相同的代碼時,它運行良好。 這是完整的代碼:

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

這里使用 Apache PDFBox - Java PDF 庫:
示例代碼:

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

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

在舊版本的 PDFBox

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

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

在較新版本的 PDFBox 中(例如 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;

我這樣做的方法是嘗試使用PdfReader讀取 PDF 文件,當然不傳遞密碼。 如果文件受密碼保護,則會拋出BadPasswordException 這是使用 iText 庫。

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

您可以使用 iText PDF 庫進行檢查。 如果它發生異常處理它(詢問密碼)

這是一個不需要 3rd 方庫的解決方案,使用 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
    }
}

參考: https : //developer.android.com/reference/android/graphics/pdf/PdfRenderer

試試這個代碼:

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

如果您的文檔受密碼保護,則它無法加載文檔並拋出 IOException。 使用pdfbox-2.0.4.jar驗證上述代碼

我不想使用任何第三方庫,所以我使用了這個 -

            try {
                new PdfRenderer(ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY));
            } catch (Exception e) {
                e.printStackTrace();
                // file is password protected
            }

如果文件受密碼保護,我就沒有使用它。

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

注意:在 iText 中有一個極端情況,一些文件被加密保護但沒有密碼打開,讀取這些文件並像這樣添加水印

PdfReader reader = new PdfReader(src);
reader.setUnethicalReading(true);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM