1.pom
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.22</version>
</dependency>
2.代码
public static String getTextFromPDF(String pdfFilePath) throws Exception {
RandomAccessRead accessRead = new RandomAccessFile(new File(pdfFilePath), "rw");
PDFParser parser = new PDFParser(accessRead); // 创建PDF解析器
parser.parse(); // 执行PDF解析过程
PDDocument pdfdocument = parser.getPDDocument(); // 获取解析器的PDF文档对象
PDFTextStripper pdfstripper = new PDFTextStripper(); // 生成PDF文档内容剥离器
String contenttxt = pdfstripper.getText(pdfdocument); // 利用剥离器获取文档
System.out.println(contenttxt);
accessRead.close();
pdfdocument.close();
return contenttxt;
}