引入依赖
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.28</version>
</dependency>
样例代码
public static String pdf2text(InputStream inputStream) {
String text = "";
try (PDDocument document = PDDocument.load(inputStream)) {
PDFTextStripper stripper = new PDFTextStripper();
text = stripper.getText(document);
} catch (IOException e) {
log.error("解析PDF文件失败!", e);
}
return text;
}