如何检查PDF是否受密码保护

时间:2013-04-09 08:15:10

标签: java passwords itext file-permissions pdf-reader

我正在尝试使用iText的PdfReader来检查给定的PDF文件是否受密码保护,但是我得到了这个例外:

  

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

但是,当针对非密码保护的文件测试相同的代码时,它运行正常。这是完整的代码:

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

8 个答案:

答案 0 :(得分:5)

旧版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;

答案 1 :(得分:4)

我这样做的方法是尝试使用PdfReader读取PDF文件而不传递密码。如果文件受密码保护,则会抛出BadPasswordException。这是使用iText库。

答案 2 :(得分:3)

使用Apache PDFBox - 来自here的Java PDF库:
示例代码:

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

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

答案 3 :(得分:2)

试试这段代码:

    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

验证了上述代码

答案 4 :(得分:2)

import { Component, OnInit } from '@angular/core';
import { NgxNavigationWithDataComponent } from 'ngx-navigation-with-data';

@Component({
 selector: 'app-about',
 templateUrl: './about.component.html',
 styleUrls: ['./about.component.css']
})
export class AboutComponent implements OnInit {

 constructor(public navCtrl: NgxNavigationWithDataComponent) {
  console.log(this.navCtrl.get('name')); // it will console Virendra
  console.log(this.navCtrl.data); // it will console whole data object here
 }

 ngOnInit() {
 }

}

使用iText PDF库可以检查。如果发生异常处理(询问密码)

答案 5 :(得分:0)

使用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

答案 6 :(得分:0)

我不想使用任何第三方库,所以我使用了这个 -

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

如果文件受密码保护,我就没有使用它。

答案 7 :(得分:0)

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