我正在尝试提取 pdf 文件中“[”和“]”之间的文本,但我无法这样做,因为该文件似乎已加密。我收到一些格式不可读的符号..
public class ITextReadDemo {
public static void main(String[] args) {
try {
PdfReader reader = new PdfReader("D:\\temp\\1.pdf");
System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");
String page = PdfTextExtractor.getTextFromPage(reader, 2);
System.out.println("Page Content:\n\n"+page+"\n\n");
System.out.println("Is this document tampered : "+reader.isTampered());
System.out.println("Is this document encrypted : "+reader.isEncrypted());
} catch (IOException e) {
e.printStackTrace();
}
}
}
但我遇到了这个异常:
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1OctetString
at com.itextpdf.text.pdf.PdfEncryption.<init>(PdfEncryption.java:147)
at com.itextpdf.text.pdf.PdfReader.readDecryptedDocObj(PdfReader.java:775)
at com.itextpdf.text.pdf.PdfReader.readDocObj(PdfReader.java:1152)
at com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:512)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:172)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:161)
at pdfexc.ITextReadDemo.main(ITextReadDemo.java:19)
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.ASN1OctetString
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more
我也尝试了以下方法。它正在从 pdf 文件中读取内容,但是当我显示它时,它不是可读的格式
void readfile() {
Path path = Paths.get("D:\\temp\\1.pdf");
Scanner scanner = new Scanner(path);
while(scanner.hasNextLine()){
String line = scanner.nextLine();
System.out.println(line);
}
}
我需要的只是 pdf 文件(不是文本文件)中的内容,因为它是可读格式的,以便我可以使用正则表达式提取黑白 [ 和 ] 文本。如果您知道解决方案,请帮助我。
最佳答案
异常已经描述了问题的原因:
Exception in thread "main" java.lang.NoClassDefFoundError: org/bouncycastle/asn1/ASN1OctetString
IText 使用 BouncyCaSTLe 库来执行加密和签名等安全相关任务,但您的类路径中似乎没有该库,或者至少没有所需的版本。
不幸的是,不要说出您使用的是哪个 iText 版本,因此我无法判断哪个 BouncyCaSTLe 版本是必需的。
关于java - 从 pdf 文件中提取文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35569880/