PDF全稱Portable Document Format,是Adobe公司開發的電子文件格式。這種文件格式與操作系統平臺無關,可以在Windows、Unix或Mac OS等操作系統上通用。
PDF文件格式將文字、字型、格式、顏色及獨立於設備和分辨率的圖形圖像等封裝在一個文件中。如果要抽取其中的文本信息,需要根據它的文件格式來進行解析。幸好目前已經有不少工具能幫助我們做這些事情。
2、PDFBox的下載
最常見的一種PDF文本抽取工具就是PDFBox了,訪問網址http://sourceforge.net/projects/pdfbox/,進入如圖7-1所示的下載界面。讀者可以在該網頁下載其最新的版本。本書採用的是PDFBox-0.7.3版本。PDFBox是一個開源的Java PDF庫,這個庫允許你訪問PDF文件的各項信息。在接下來的例子中,將演示如何使用PDFBox提供的API,從一個PDF文件中提取出文本信息。
3、在Eclipse中配置
以下是在Eclipse中創建工程,並建立解析PDF文件的工具類的過程。
(1)在Eclipse的workspace中創建一個普通的Java工程:ch7。
(2)把下載的PDFBox-0.7.3.zip解壓。
(3)進入external目錄下,可以看到,這裏包括了PDFBox所有用到的外部包。複製下面的Jar包到工程ch7的lib目錄下(如還未建立lib目錄,則先創建一個)。
l bcmail-jdk14-132.jar
l bcprov-jdk14-132.jar
l checkstyle-all-4.2.jar
l FontBox-0.1.0-dev.jar
l lucene-core-2.0.0.jar
然後再從PDFBox的lib目錄下,複製PDFBox-0.7.3.jar到工程的lib目錄下。
(4)在工程上單擊右鍵,在彈出的快捷菜單中選擇“Build Path->Config Build Path->Add Jars”命令,把工程lib目錄下面的包都加入工程的Build Path。
4、主要類介紹:
PDFTextStripper:核心提取組件,包含設置是否排序、抽取的起始頁和結束頁等方法。
PDDocument:內存中保存的PDFDocument對象,提供從文件系統和鏈接地址裝入文件兩種方式。
LucenePDFDocument:是主要用於和lucene集成的類,提供getDocument方法(也重載爲接收url和本地文件兩種方式),將PDF文件轉化爲lucene中的Document對象,並自動提取字段加入Document,可直接通過IndexWriter將其寫入索引中。
5、使用PDFBox解析PDF內容
在剛剛創建的Eclipse工程中,創建一個ch7.pdfbox包,並創建一個PdfboxTest類。該類包含一個getText方法,用於從一個PDF中獲取文本信息,其代碼如下。
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import org.pdfbox.pdfparser.PDFParser;
import org.pdfbox.util.PDFTextStripper;
public class PdfParser {
/**
* @param args
*/
// TODO 自動生成方法存根
public static void main(String[] args) throws Exception{
PDDocument doc = PDDocument.load("d:/文檔.pdf");
int pagenumber = doc.getNumberOfPages();
FileOutputStream fos = new FileOutputStream("d:/文檔.txt");
Writer writer = new OutputStreamWriter(fos,"UTF-8");
PDFTextStripper stripper = new PDFTextStripper();
ts.setStortByPosition(false);
// stripper.setWordSeparator(""); //這樣中文輸出就不會帶空格
stripper.setStartPage(1);
stripper.setEndPage(4);
stripper.writeText(doc,writer);
doc.close();
writer.close();
}
}
下面我們來看一個索引類
package com.qianyan.pdf;
import java.io.File;
import java.io.IOException;
import net.paoding.analysis.analyzer.PaodingAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
public class TestLucenePDFDocument {
public static void main(String[] args) throws IOException{
String indexDir = "d:/luceneindex";
Analyzer analyzer = new PaodingAnalyzer();
IndexWriter writer = new IndexWriter(indexDir, analyzer, true);
Document doc = LucenePDFDocument.getDocument(new File("d:/explain.pdf"));
writer.addDocument(doc);
writer.close;
}
}
下面介紹解析PDF的另一種方式 (XPDF處理PDF文檔)
1、首先需要去下載相應包,XPDF官網Download (xpdfbin-win-3.03.zip、xpdf-chinese-simplified.tar.gz)
2、配置:
1)在這裏把解壓後的xpdf-chinese-simplified拷貝到解壓後xpdfbin-win-3.03的doc目錄下
2)找到doc目錄下的sample-xpdfrc的文件,改名爲xpdfrc,然後打開進行編輯 :
1)#textEncoding UTF-8 (找到此行,將前面的#號去掉)
2)文件末尾添加以下代碼(注意:目錄爲相應解壓目錄):
3)代碼實現,爲了方便我們運行命令行的xpdf,我們封裝了一個XpdfParams類
package com.qianyan.xpdf;
public class XpdfParams {
private String convertor = "";
private String layout = "";
private String encoding = "";
private String source = "";
private String target = "";
public String getConvertor() {
return convertor;
}
public void setConvertor(String convertor) {
this.convertor = convertor;
}
public String getLayout() {
return layout;
}
public void setLayout(String layout) {
this.layout = layout;
}
public String getEncoding() {
return encoding;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
public String getCMD(){
return convertor + " " + layout + " " + encoding + " " + source + " " + target + " ";
}
}
我們來看TestRuntime類,該類通過java的運行類查找我們配置好的xpdf.exe然後根據要求轉換pdf到txt文本中。
package com.qianyan.xpdf;
import java.io.IOException;
public class TestRuntime {
public static void main(String[] args) throws IOException{
XpdfParams xparam = new XpdfParams();
xparam.setConvertor("D:\\program files (x86)\\xpdf\\bin64\\pdftotext.exe");
xparam.setEncoding("-enc UTF-8");
xparam.setSource("E:\\lucene.pdf");
xparam.setTarget("E:\\lucene.txt");
String cmd = xparam.getCMD();
Runtime.getRuntime().exec(cmd);
}
}
解析PDF到此結束,下章我們來學習對HTML的解析 。