1.功能描述;
此功能主要功能就是在pdf文件中读取信息后查找指定页面的指定关键信息,获得相应的坐标。
首先要 itextpdf.jar, itext.jar包
//获取pdf的页数
PdfReader reader= new PdfReader(generatePath);
int pagecount= reader.getNumberOfPages();
/**
在文件中寻找指定的文字内容
@param reader
@param pageNumber
@param keyword
@return
@throws Exception
*/
public static MatchItem matchPage(com.itextpdf.text.pdf.PdfReader reader,
Integer pageNumber, String keyword) throws Exception {
KeyWordPositionListener renderListener = new KeyWordPositionListener();
renderListener.setKeyword(keyword);
PdfReaderContentParser parse = new PdfReaderContentParser(reader);
Rectangle rectangle = reader.getPageSize(pageNumber);
renderListener.setPageNumber(pageNumber);
renderListener.setCurPageSize(rectangle);
parse.processContent(pageNumber, renderListener);
MatchItem matchItem = findKeywordItems(renderListener, keyword);
// 当前页找不到,到前一页查询
import com.itextpdf.awt.geom.Rectangle2D;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.parser.ImageRenderInfo;
import com.itextpdf.text.pdf.parser.RenderListener;
import com.itextpdf.text.pdf.parser.TextRenderInfo;
import java.util.ArrayList;
import java.util.List;
public class KeyWordPositionListener implements RenderListener {
private List
private List
private Rectangle curPageSize;
/**
* 匹配的关键字
*/
private String keyword;
/**
* 匹配的当前页
*/
private Integer pageNumber;
public void beginTextBlock() {
//do nothing
}
public void renderText(TextRenderInfo renderInfo) {
String content = renderInfo.getText();
content = content.replace("<", "").replace("《", "").replace("(", "").replace("(", "").replace("\"", "").replace("'", "")
.replace(">", "").replace("》", "").replace(")", "").replace(")", "").replace("、", "").replace(".", "")
.replace(":", "").replace(":", "").replace(" ", "");
Rectangle2D.Float textRectangle = renderInfo.getDescentLine().getBoundingRectange();
MatchItem item = new MatchItem();
item.setContent(content);
item.setPageNum(pageNumber);
item.setPageWidth(curPageSize.getWidth());
item.setPageHeight(curPageSize.getHeight());
item.setX((float)textRectangle.getX());
item.setY((float)textRectangle.getY());
if(content!=null && content!=""){
if(content.equalsIgnoreCase(keyword)) {
matches.add(item);
}
}else{
item.setContent("空字符串");
}
allItems.add(item);//先保存所有的项
}
public void endTextBlock() {
//do nothing
}
public void renderImage(ImageRenderInfo renderInfo) {
//do nothing
}
/**
* 设置需要匹配的当前页
* @param pageNumber
*/
public void setPageNumber(Integer pageNumber) {
this.pageNumber = pageNumber;
}
/**
* 设置需要匹配的关键字,忽略大小写
* @param keyword
*/
public void setKeyword(String keyword) {
this.keyword = keyword;
}
/**
* 返回匹配的结果列表
* @return
*/
public List
return matches;
}
void setCurPageSize(Rectangle rect) {
this.curPageSize = rect;
}
public List
return allItems;
}
public void setAllItems(List
this.allItems = allItems;
}
}
public class MatchItem {
private Integer pageNum;
private Float x;
private Float y;
private Float pageWidth;
private Float pageHeight;
private String content;
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public Float getX() {
return x;
}
public void setX(Float x) {
this.x = x;
}
public Float getY() {
return y;
}
public void setY(Float y) {
this.y = y;
}
public Float getPageWidth() {
return pageWidth;
}
public void setPageWidth(Float pageWidth) {
this.pageWidth = pageWidth;
}
public Float getPageHeight() {
return pageHeight;
}
public void setPageHeight(Float pageHeight) {
this.pageHeight = pageHeight;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "MatchItem [pageNum=" + pageNum + ", x=" + x + ", y=" + y
+ ", pageWidth=" + pageWidth + ", pageHeight=" + pageHeight
+ ", content=" + content + "]";
}
}
这样就可以在调用这个方法后获取matchPage改关键字的坐标了。