Apache PDFbox開發指南之PDF文件讀取
阿新 • • 發佈:2019-01-09
相關文章:
1、介紹
Apache PDFbox是一個開源的、基於Java的、支援PDF文件生成的工具庫,它可以用於建立新的PDF文件,修改現有的PDF文件,還可以從PDF文件中提取所需的內容。Apache PDFBox還包含了數個命令列工具。
Apache PDFbox於2016年4月26日釋出了最新的2.0.1版。
備註:本文程式碼均是基於2.0及以上版本編寫。
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);
}
}
}
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);
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);
}
3.4.2、過程說明
此方法可以取出源PDF中圖片物件PDImageXObject,然後可以對該物件進行相關處理,本程式碼實現了將提取出來的每一個圖片物件,插入到一個空白的PDF文件中。
有一點需要說明,以上程式碼註釋部分本意是想直接生成圖片檔案,但嘗試後發現檔案異常。因此大家在這個程式碼基礎上有新的想法可以繼續嘗試。
3.4.3、執行結果
源PDF檔案中包含19張圖片
分別生成19個僅包含單獨圖片的PDF
4、小結
本文僅介紹了利用Apache PDFbox相關開發包讀取PDF文字,其他複雜功能暫未涉及,需要大家自己線下探索、嘗試。