1、使用PDFBox处理PDF文档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、使用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{ FileInputStream fis = new FileInputStream("F:\\task\\lerman-atem2001.pdf"); BufferedWriter writer = new BufferedWriter(new FileWriter("F:\\task\\pdf_change.txt")); PDFParser p = new PDFParser(fis); p.parse(); PDFTextStripper ts = new PDFTextStripper(); String s = ts.getText(p.getPDDocument()); writer.write(s); System.out.println(s); fis.close(); writer.close(); } } |

1
package TestPDF.pdfbox;2

3
import java.io.File;4
import java.io.FileOutputStream;5
import java.io.IOException;6
import java.io.OutputStreamWriter;7
import java.io.Writer;8
import java.net.URL;9

10
import org.apache.lucene.analysis.standard.StandardAnalyzer;11
import org.apache.lucene.document.Document;12
import org.apache.lucene.index.IndexWriter;13
import org.apache.lucene.index.Term;14
import org.apache.lucene.search.IndexSearcher;15
import org.apache.lucene.search.PhraseQuery;16
import org.apache.lucene.search.Query;17
import org.apache.lucene.search.ScoreDoc;18
import org.apache.lucene.search.TermQuery;19
import org.apache.lucene.search.TopDocCollector;20
import org.apache.lucene.search.TopDocs;21
import org.pdfbox.pdmodel.PDDocument;22
import org.pdfbox.searchengine.lucene.LucenePDFDocument;23
import org.pdfbox.util.PDFTextStripper;24

25

public class Test
{26

27

public void getText(String file) throws Exception
{28
//是否排序29
boolean sort = false;30
//pdf文件名31
String pdfFile = file;32
//输入文本文件名称33
String textFile = null;34
//编码方式35
String encoding = "UTF-8";36
//开始提取页数37
int startPage = 1;38
//结束提取页数39
int endPage = Integer.MAX_VALUE;40
//文件输入流,输入文本文件41
Writer output = null; 42
//内存中存储的PDF Document43
PDDocument document = null;44
45

try
{46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}finally
{77

if(output != null)
{78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}83
} 84
}85
86

/** *//**87
* test Lucene with pdfbox88
* @throws IOException89
*/90

public void LuceneTest() throws IOException
{91
92
String path = "D:\\index";93
String pdfpath = "D:\\index\\Lucene.Net基本用法.pdf";94
95
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);96
//writer.setMaxFieldLength(10240);97
//LucenePDFDocument返回由PDF产生的Lucene Document98
Document d = LucenePDFDocument.getDocument(new File(pdfpath));99
//System.out.println(d);100
//写入索引101
writer.addDocument(d);102
writer.close();103
104
//读取d:\index下的索引文件,建立IndexSearcher105
IndexSearcher searcher = new IndexSearcher(path);106
//对索引的contents Field进行关键字Query的查找107
Term t = new Term("contents","优");26

27

public void getText(String file) throws Exception
{28
//是否排序29
boolean sort = false;30
//pdf文件名31
String pdfFile = file;32
//输入文本文件名称33
String textFile = null;34
//编码方式35
String encoding = "UTF-8";36
//开始提取页数37
int startPage = 1;38
//结束提取页数39
int endPage = Integer.MAX_VALUE;40
//文件输入流,输入文本文件41
Writer output = null; 42
//内存中存储的PDF Document43
PDDocument document = null;44
45

try
{46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}finally
{77

if(output != null)
{78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}83
} 84
}28
//是否排序29
boolean sort = false;30
//pdf文件名31
String pdfFile = file;32
//输入文本文件名称33
String textFile = null;34
//编码方式35
String encoding = "UTF-8";36
//开始提取页数37
int startPage = 1;38
//结束提取页数39
int endPage = Integer.MAX_VALUE;40
//文件输入流,输入文本文件41
Writer output = null; 42
//内存中存储的PDF Document43
PDDocument document = null;44
45

try
{46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
}53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}finally
{77

if(output != null)
{78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}83
}77

if(output != null)
{78
output.close(); 79
}78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}81
document.close();82
}83
} 84
}85
86

/** *//**87
* test Lucene with pdfbox88
* @throws IOException89
*/87
* test Lucene with pdfbox88
* @throws IOException89
*/90

public void LuceneTest() throws IOException
{91
92
String path = "D:\\index";93
String pdfpath = "D:\\index\\Lucene.Net基本用法.pdf";94
95
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);96
//writer.setMaxFieldLength(10240);97
//LucenePDFDocument返回由PDF产生的Lucene Document98
Document d = LucenePDFDocument.getDocument(new File(pdfpath));99
//System.out.println(d);100
//写入索引101
writer.addDocument(d);102
writer.close();103
104
//读取d:\index下的索引文件,建立IndexSearcher105
IndexSearcher searcher = new IndexSearcher(path);106
//对索引的contents Field进行关键字Query的查找107
Term t = new Term("contents","优");91
92
String path = "D:\\index";93
String pdfpath = "D:\\index\\Lucene.Net基本用法.pdf";94
95
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);96
//writer.setMaxFieldLength(10240);97
//LucenePDFDocument返回由PDF产生的Lucene Document98
Document d = LucenePDFDocument.getDocument(new File(pdfpath));99
//System.out.println(d);100
//写入索引101
writer.addDocument(d);102
writer.close();103
104
//读取d:\index下的索引文件,建立IndexSearcher105
IndexSearcher searcher = new IndexSearcher(path);106
//对索引的contents Field进行关键字Query的查找107
Term t = new Term("contents","优");26

27

public void getText(String file) throws Exception
{28
//是否排序29
boolean sort = false;30
//pdf文件名31
String pdfFile = file;32
//输入文本文件名称33
String textFile = null;34
//编码方式35
String encoding = "UTF-8";36
//开始提取页数37
int startPage = 1;38
//结束提取页数39
int endPage = Integer.MAX_VALUE;40
//文件输入流,输入文本文件41
Writer output = null; 42
//内存中存储的PDF Document43
PDDocument document = null;44
45

try
{46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}finally
{77

if(output != null)
{78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}83
} 84
}28
//是否排序29
boolean sort = false;30
//pdf文件名31
String pdfFile = file;32
//输入文本文件名称33
String textFile = null;34
//编码方式35
String encoding = "UTF-8";36
//开始提取页数37
int startPage = 1;38
//结束提取页数39
int endPage = Integer.MAX_VALUE;40
//文件输入流,输入文本文件41
Writer output = null; 42
//内存中存储的PDF Document43
PDDocument document = null;44
45

try
{46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
}53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}finally
{77

if(output != null)
{78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}83
}77

if(output != null)
{78
output.close(); 79
}78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}81
document.close();82
}83
} 84
}28
//是否排序29
boolean sort = false;30
//pdf文件名31
String pdfFile = file;32
//输入文本文件名称33
String textFile = null;34
//编码方式35
String encoding = "UTF-8";36
//开始提取页数37
int startPage = 1;38
//结束提取页数39
int endPage = Integer.MAX_VALUE;40
//文件输入流,输入文本文件41
Writer output = null; 42
//内存中存储的PDF Document43
PDDocument document = null;44
45

try
{46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
}53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}46

try
{47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
}53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}47
//首先当作一个URL来加载文件,如果得到异常再从本地系统装载文件48
URL url = new URL(pdfFile);49
document = PDDocument.load(url);50
String fileName = url.getFile();51
52

if(fileName.length() > 4)
{53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
}53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
}53
//以原来pdf名称来命名新产生的txt文件54
File outputFile = new File(fileName.substring(0, fileName.length()-4) + ".txt");55
textFile = outputFile.getName();56
} 57

}catch(Exception e)
{58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}58
//如果作为URL装载得到异常则从文件系统装载59
document = PDDocument.load(pdfFile);60

if(pdfFile.length() > 4)
{61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}61
textFile = pdfFile.substring(0, pdfFile.length() - 4) + ".txt";62
}63
}64
//文件输出流,写入文件到textFile65
output = new OutputStreamWriter(new FileOutputStream(textFile),encoding);66
//PDFTextStripper来提取文本67
PDFTextStripper stripper = new PDFTextStripper();68
//设置是否排序69
stripper.setSortByPosition(sort);70
//设置起始页71
stripper.setStartPage(startPage);72
//设置结束页73
stripper.setEndPage(endPage);74
//调用PDFTextStripper的writeText提取并输出文本75
stripper.writeText(document, output);76

}finally
{77

if(output != null)
{78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}83
}77

if(output != null)
{78
output.close(); 79
}78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}81
document.close();82
}83
}77

if(output != null)
{78
output.close(); 79
}78
output.close(); 79
}78
output.close(); 79
}80

if(document != null)
{81
document.close();82
}81
document.close();82
}81
document.close();82
}83
} 84
}85
86

/** *//**87
* test Lucene with pdfbox88
* @throws IOException89
*/87
* test Lucene with pdfbox88
* @throws IOException89
*/87
* test Lucene with pdfbox88
* @throws IOException89
*/90

public void LuceneTest() throws IOException
{91
92
String path = "D:\\index";93
String pdfpath = "D:\\index\\Lucene.Net基本用法.pdf";94
95
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);96
//writer.setMaxFieldLength(10240);97
//LucenePDFDocument返回由PDF产生的Lucene Document98
Document d = LucenePDFDocument.getDocument(new File(pdfpath));99
//System.out.println(d);100
//写入索引101
writer.addDocument(d);102
writer.close();103
104
//读取d:\index下的索引文件,建立IndexSearcher105
IndexSearcher searcher = new IndexSearcher(path);106
//对索引的contents Field进行关键字Query的查找107
Term t = new Term("contents","优");91
92
String path = "D:\\index";93
String pdfpath = "D:\\index\\Lucene.Net基本用法.pdf";94
95
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);96
//writer.setMaxFieldLength(10240);97
//LucenePDFDocument返回由PDF产生的Lucene Document98
Document d = LucenePDFDocument.getDocument(new File(pdfpath));99
//System.out.println(d);100
//写入索引101
writer.addDocument(d);102
writer.close();103
104
//读取d:\index下的索引文件,建立IndexSearcher105
IndexSearcher searcher = new IndexSearcher(path);106
//对索引的contents Field进行关键字Query的查找107
Term t = new Term("contents","优");91
92
String path = "D:\\index";93
String pdfpath = "D:\\index\\Lucene.Net基本用法.pdf";94
95
IndexWriter writer = new IndexWriter(path, new StandardAnalyzer(),true);96
//writer.setMaxFieldLength(10240);97
//LucenePDFDocument返回由PDF产生的Lucene Document98
Document d = LucenePDFDocument.getDocument(new File(pdfpath));99
//System.out.println(d);100
//写入索引101
writer.addDocument(d);102
writer.close();103
104
//读取d:\index下的索引文件,建立IndexSearcher105
IndexSearcher searcher = new IndexSearcher(path);106
//对索引的contents Field进行关键字Query的查找107
Term t = new Term("contents","优");108
Term m = new Term("contents","化");109
PhraseQuery q = new PhraseQuery();110
q.add(t);111
q.add(m);112
//Query q = new TermQuery(t);113
TopDocCollector co = new TopDocCollector(10);114
searcher.search(q,co);115
116
Document document;117
TopDocs docs = co.topDocs();118
ScoreDoc[] doc = docs.scoreDocs;119
//System.out.println(doc.length);120
121

for(int i=0;i<doc.length;i++)
{122
System.out.println("文档编号:" + doc[i].doc);123
//document = searcher.doc(doc[i].doc);124
}122
System.out.println("文档编号:" + doc[i].doc);123
//document = searcher.doc(doc[i].doc);124
}122
System.out.println("文档编号:" + doc[i].doc);123
//document = searcher.doc(doc[i].doc);124
}125
}126

/** *//**127
* @param args128
*/127
* @param args128
*/127
* @param args128
*/129

public static void main(String[] args)
{130
// TODO Auto-generated method stub131
Test test = new Test();132

try
{133
//test.getText("D:\\index\\Lucene.Net基本用法.pdf");134
test.LuceneTest();135

}catch(Exception e)
{136
e.printStackTrace();137
}130
// TODO Auto-generated method stub131
Test test = new Test();132

try
{133
//test.getText("D:\\index\\Lucene.Net基本用法.pdf");134
test.LuceneTest();135

}133
//test.getText("D:\\index\\Lucene.Net基本用法.pdf");134
test.LuceneTest();135

}catch(Exception e)
{136
e.printStackTrace();137
}136
e.printStackTrace();137
}130
// TODO Auto-generated method stub131
Test test = new Test();132

try
{133
//test.getText("D:\\index\\Lucene.Net基本用法.pdf");134
test.LuceneTest();135

}133
//test.getText("D:\\index\\Lucene.Net基本用法.pdf");134
test.LuceneTest();135

}133
//test.getText("D:\\index\\Lucene.Net基本用法.pdf");134
test.LuceneTest();135

}catch(Exception e)
{136
e.printStackTrace();137
}136
e.printStackTrace();137
}136
e.printStackTrace();137
}138
}139
}140
8048

被折叠的 条评论
为什么被折叠?



