1. 程式人生 > >java 讀取PDF

java 讀取PDF

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頁