Apache PDFbox開發指南之PDF文檔讀取


轉載請注明來源:http://blog.csdn.net/loongshawn/article/details/51542309

相關文章:

1、介紹

Apache PDFbox是一個開源的、基於Java的、支持PDF文檔生成的工具庫,它可以用於創建新的PDF文檔,修改現有的PDF文檔,還可以從PDF文檔中提取所需的內容。Apache PDFBox還包含了數個命令行工具。 
Apache PDFbox於2016年4月26日發布了最新的2.0.1版。

備注:本文代碼均是基於2.0及以上版本編寫。

官網地址:https://pdfbox.apache.org/index.html

PDFBox 2.0.1 API在線文檔:https://pdfbox.apache.org/docs/2.0.1/javadocs/

2、特征

Apache PDFBox主要有以下特征: 
PDF讀取、創建、打印、轉換、驗證、合並分割等特征。

3、開發實戰

3.1、場景說明

  • 1、讀取PDF文本內容,樣例中為讀取體檢報告文本內容。

  • 2、提取PDF文檔中的圖片。這里僅僅實現將PDF中的圖片另存為一個單獨的PDF,至於需要直接輸出圖片文件(暫時沒有實現),大家可以參考我的代碼加以拓展,主要就是處理PDImageXObject對象。

3.2、所需jar包

pdfbox-2.0.1.jar下載地址

fontbox-2.0.1.jar下載地址

將上述兩jar包添加到工程庫中,如下: 
這里寫圖片描述

3.3、文本內容提取

3.3.1、文本內容提取

創建PdfReader類,編寫下述功能函數。

package com.loongshaw; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import org.apache.pdfbox.io.RandomAccessBuffer; import org.apache.pdfbox.pdfparser.PDFParser; import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; public class PdfReader { public static void main(String[] args){ File pdfFile = new File("/Users/dddd/Downloads/0571888890423433356rrrr_182-93201510313223336-2.pdf"); PDDocument document = null; try { // 方式一: /** InputStream input = null; input = new FileInputStream( pdfFile ); //加載 pdf 文檔 PDFParser parser = new PDFParser(new RandomAccessBuffer(input)); parser.parse(); document = parser.getPDDocument(); **/ // 方式二: document=PDDocument.load(pdfFile); // 獲取頁碼 int pages = document.getNumberOfPages(); // 讀文本內容 PDFTextStripper stripper=new PDFTextStripper(); // 設置按順序輸出 stripper.setSortByPosition(true); stripper.setStartPage(1); stripper.setEndPage(pages); String content = stripper.getText(document); System.out.println(content); } catch(Exception e) { System.out.println(e); } } }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

3.3.2、過程說明

PDF文件加載有兩種方式,無明顯差異,方式二代碼較簡潔:

// 方式一: InputStream input = null; input = new FileInputStream( pdfFile ); //加載 pdf 文檔 PDFParser parser = new PDFParser(new RandomAccessBuffer(input)); parser.parse(); document = parser.getPDDocument(); // 方式二: document=PDDocument.load(pdfFile); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

3.3.3、執行結果

這里寫圖片描述

3.4、圖片提取(2016-12-02添加)

3.3.1、圖片提取

public static void readImage(){ // 待解析PDF File pdfFile = new File("/Users/xiaolong/Downloads/test.pdf"); // 空白PDF File pdfFile_out = new File("/Users/xiaolong/Downloads/testout.pdf"); PDDocument document = null; PDDocument document_out = null; try { document = PDDocument.load(pdfFile); document_out = PDDocument.load(pdfFile_out); } catch (IOException e) { e.printStackTrace(); } int pages_size = document.getNumberOfPages(); System.out.println("getAllPages==============="+pages_size); int j=0; for(int i=0;i<pages_size;i++) { PDPage page = document.getPage(i); PDPage page1 = document_out.getPage(0); PDResources resources = page.getResources(); Iterable xobjects = resources.getXObjectNames(); if (xobjects != null) { Iterator imageIter = xobjects.iterator(); while (imageIter.hasNext()) { COSName key = (COSName) imageIter.next(); if(resources.isImageXObject(key)){ try { PDImageXObject image = (PDImageXObject) resources.getXObject(key); // 方式一:將PDF文檔中的圖片 分別存到一個空白PDF中。 PDPageContentStream contentStream = new PDPageContentStream(document_out,page1,AppendMode.APPEND,true); float scale = 1f; contentStream.drawImage(image, 20,20,image.getWidth()*scale,image.getHeight()*scale); contentStream.close(); document_out.save("/Users/xiaolong/Downloads/123"+j+".pdf"); System.out.println(image.getSuffix() + ","+image.getHeight() +"," + image.getWidth()); /** // 方式二:將PDF文檔中的圖片 分別另存為圖片。 File file = new File("/Users/xiaolong/Downloads/123"+j+".png"); FileOutputStream out = new FileOutputStream(file); InputStream input = image.createInputStream(); int byteCount = 0; byte[] bytes = new byte[1024]; while ((byteCount = input.read(bytes)) > 0) { out.write(bytes,0,byteCount); } out.close(); input.close(); **/ } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //image count j++; } } } } System.out.println(j); } 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77

3.4.2、過程說明

此方法可以取出源PDF中圖片對象PDImageXObject,然后可以對該對象進行相關處理,本代碼實現了將提取出來的每一個圖片對象,插入到一個空白的PDF文檔中。

有一點需要說明,以上代碼注釋部分本意是想直接生成圖片文件,但嘗試后發現文件異常。因此大家在這個代碼基礎上有新的想法可以繼續嘗試。

3.4.3、執行結果

這里寫圖片描述 
源PDF文件中包含19張圖片

這里寫圖片描述 
分別生成19個僅包含單獨圖片的PDF

4、小結

本文僅介紹了利用Apache PDFbox相關開發包讀取PDF文本,其他復雜功能暫未涉及,需要大家自己線下探索、嘗試。

 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM