1.首先引進pom
<!-- PDF讀取依賴 -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
2.controller層直接代碼
/**
* PDF解析
* @return
*/
@PostMapping("/getPdf")
public StringBuffer getPdf(@RequestBody JSONObject jsonObject) throws IOException {
StringBuffer stringBuffer = null;
//獲取服務器地址
ImportParams params = new ImportParams();
params.setSaveUrl("/file");
String filePath = jsonObject.getString("filePath");
filePath = fileServer + "/" + filePath;
URL url = new URL(filePath);
URLConnection connection = url.openConnection();
InputStream inputStream = connection.getInputStream();
try {
PDDocument document;
PDFParser parser = new PDFParser(new RandomAccessBuffer(inputStream));
parser.parse();
document = parser.getPDDocument();
document.getClass();
if(!document.isEncrypted()) {
PDFTextStripperByArea stripper = new PDFTextStripperByArea();
stripper.setSortByPosition(true);
PDFTextStripper textStripper = new PDFTextStripper();
String exposeContent = textStripper.getText(document);
String[] content = exposeContent.split("\\n");
stringBuffer = new StringBuffer();
for(String line:content) {
stringBuffer.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return stringBuffer;
}