小智 24
使用Apache PDFBox,它是这样的:
PDDocument document = PDDocument.load(new File("test.pdf"));
if (!document.isEncrypted()) {
PDFTextStripper stripper = new PDFTextStripper();
String text = stripper.getText(document);
System.out.println("Text:" + text);
}
document.close();
Run Code Online (Sandbox Code Playgroud)
Bol*_*olo 17
iText对文本操作有更多的低级支持,但是您必须编写大量代码才能获取文本.
iText in Action包含对PDF文本提取限制的很好概述,无论使用哪个库(第18.2节:提取和编辑文本),以及为什么库没有文本提取支持的令人信服的解释.简而言之,编写一个处理简单案例的代码相对容易,但基本上不可能从PDF中提取文本.