java 讀取PDF
阿新 • • 發佈:2018-11-08
pom.xml配置
<!-- 讀取pdf檔案 -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.4</version>
</dependency>
java程式碼:
private static String pdfRead(File file) { String content = null; PDDocument document = null; try { document = PDDocument.load(file); PDFTextStripper pts = new PDFTextStripper(); pts.setStartPage(1); pts.setEndPage(10); content = pts.getText(document); } catch (Exception e) { e.printStackTrace(); } finally { if (null != document) { try { document.close(); } catch (IOException e) { e.printStackTrace(); } } } return content; }
讀成string型別,讀取1到10頁