lucene入门-解析pdf(使用pdfbox解析英文PDF)
下载pdfbox
http://incubator.apache.org/pdfbox/
下载相关的jar
http://commons.apache.org/downloads/download_logging.cgi
引入external下的所有包
英语PDF:
package extract;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.*;
public class ExtractorPDF {
public static String getText(String file){
String s="";
String pdffile=file;
PDDocument pdfdoc=null;
try {
pdfdoc=PDDocument.load(pdffile);
PDFTextStripper stripper=new PDFTextStripper();
s=stripper.getText(pdfdoc);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if (pdfdoc!=null){
pdfdoc.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return s;
}
public static void toTextFile(String doc,String filename) throws Exception{
String pdffile=doc;
PDDocument pdfdoc=PDDocument.load(doc);
try {
pdfdoc=PDDocument.load(pdffile);
PDFTextStripper stripper=new PDFTextStripper();
PrintWriter pw=new PrintWriter(new FileWriter(filename));
stripper.writeText(pdfdoc, pw);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try {
if (pdfdoc!=null){
pdfdoc.close();
}
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
String sc=getText("D:/workspace/testsearch2/htmls/xxxx.pdf");
System.out.print(sc);
toTextFile("D:/workspace/testsearch2/htmls/xxxx.pdf","D:/workspace/testsearch2/htmls/xxxx.txt");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
先读取PDF文件内容输出
然后再将该PDF文件转换成TXT
上一篇: 数组扁平化解析