java 如何检查 PDF 是否受密码保护
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15896691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to check if a PDF is Password Protected or not
提问by tusharagrawa
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:
我正在尝试使用 iText 的 PdfReader 检查给定的 PDF 文件是否受密码保护,但出现此异常:
Exception in thread "Main Thread" java.lang.NoClassDefFoundError:org/bouncycastle/asn1/ASN1OctetString
线程“主线程”中的异常 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();
}
采纳答案by Shreyos Adikari
回答by Waleed Madanat
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.
我这样做的方法是尝试在PdfReader
不传递密码的情况下尝试读取 PDF 文件。如果文件受密码保护,BadPasswordException
则会抛出 a。这是使用 iText 库。
回答by VHS
In the old version of PDFBox
在旧版本的 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)
在较新版本的 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;
回答by Nitin Vavdiya
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
如果您的文档受密码保护,则它无法加载文档并抛出 IOException。使用上面的代码验证pdfbox-2.0.4.jar
回答by Shivam
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)
您可以使用 iText PDF 库进行检查。如果它进入异常处理它(询问密码)
回答by akodiakson
Here's a solution that doesn't require 3rd party libraries, using the PdfRenderer API.
这是一个不需要 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
}
}
Reference: https://developer.android.com/reference/android/graphics/pdf/PdfRenderer
参考:https: //developer.android.com/reference/android/graphics/pdf/PdfRenderer