javaitextpdf签章根据关键字定位在pdf⽂件中的坐标
最近接到个需求,根据所传关键字,定位关键字所在pdf⽂件中的坐标,然后盖章,其中所传参数还有关键字所在页数,和获取第⼏次出现的关键字,会传多个印章和关键字,每个印章对应⼀个关键字。⾃⼰先写了个简单demo,和⼤家共享,还请多多指正。
2.bean;
public class KeyWordBean implements Comparable<KeyWordBean> { public KeyWordBean() {
super();
}
public KeyWordBean(float x, float y, int page, String text) {
super();
this.x = x;
this.y = y;
this.page = page;
< = text;
}
private float x;
private float y;
private int page;网恋情书
private String text;
public float getX() {
return x;
}
public void tX(float x) {
this.x = x;
}
public float getY() {
return y;
}
public void tY(float y) {
this.y = y;
}
public int getPage() {
return page;
}
public void tPage(int page) {
this.page = page;
}
public String getText() {
return text;
}
public void tText(String text) {
< = text;
}
@Override
public String toString() {
return "KeyWordBean [x=" + x + ", y=" + y + ", page=" + page
+ ", text=" + text + "]";
}
@Override
public int compareTo(KeyWordBean o) {
int i = (int) (o.getY() - Y());//先按照Y轴排序
if(i == 0){
return (int) (this.x - o.getX());//如果Y轴相等了再按X轴进⾏排序
}
return i;
}
}
2.bean;
public class SignPDFBean {
private String keyWord; // 签章关键字
private int page; // 关键字所在页数
private int num; // 取第n次出现的关键字
private String alPath; // 印章图⽚路径
private String keyStorePath; // 证书⽂件路径
private String keyStorePass; // 证书密码
private String signReason; // 设置签章原因,可以为空
private String signLocation; // 设置签章地点
public String getKeyWord() {
return keyWord;
}
public void tKeyWord(String keyWord) {
this.keyWord = keyWord;
}
public int getPage() {
return page;
}
public void tPage(int page) {
this.page = page;
}
public int getNum() {light的意思
return num;
}
public void tNum(int num) {
this.num = num;
}
public String getSealPath() {
return alPath;
}
public void tSealPath(String alPath) {
this.alPath = alPath;
}
public String getKeyStorePath() {
return keyStorePath;婴儿眼屎多
}
public void tKeyStorePath(String keyStorePath) {
this.keyStorePath = keyStorePath;
}
public String getKeyStorePass() {
return keyStorePass;
}
public void tKeyStorePass(String keyStorePass) {
this.keyStorePass = keyStorePass;
}
public String getSignReason() {
return signReason;
}
public void tSignReason(String signReason) {
this.signReason = signReason;
}
public String getSignLocation() {
return signLocation;
}
public void tSignLocation(String signLocation) {
this.signLocation = signLocation;
}
@Override
谢觉哉public String toString() {
return "SignPDFBean [keyWord=" + keyWord + ", page=" + page + ", num=" + num + ", alPath=" + alPath + ", keyStorePath="
+ keyStorePath + ", keyStorePass=" + keyStorePass
+ ", signReason=" + signReason + ", signLocation="
+ signLocation + "]";
}
}
2.bean;
import java.util.List;
public class SignPDFRequestBean {
private String srcPDFPath; // 待签章pdf⽂件路径
private String outPDFPath; // 签章后输出的pdf⽂件路径
private List<SignPDFBean> SignPDFBeans;
public String getSrcPDFPath() {
return srcPDFPath;
}
public void tSrcPDFPath(String srcPDFPath) {
this.srcPDFPath = srcPDFPath;
}
public String getOutPDFPath() {
return outPDFPath;
}
public void tOutPDFPath(String outPDFPath) {
this.outPDFPath = outPDFPath;
}
public List<SignPDFBean> getSignPDFBeans() {
return SignPDFBeans;
}
public void tSignPDFBeans(List<SignPDFBean> signPDFBeans) {
SignPDFBeans = signPDFBeans;
}
@Override
public String toString() {
return "SignPDFRequestBean [srcPDFPath=" + srcPDFPath + ", outPDFPath="
+ outPDFPath + ", SignPDFBeans=" + SignPDFBeans + "]";
}
}
2;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import s.lang.StringUtils;
2.bean.KeyWordBean;
llect.Lists;
import pdf.PdfReader;
import pdf.parr.ImageRenderInfo;
import pdf.parr.PdfReaderContentParr;
import pdf.parr.RenderListener;
import pdf.parr.TextRenderInfo;
public class KeywordPDFUtils {
/
/ 定义返回页码
private static int i = 0;
private static com.Rectangle2D.Float boundingRectange =null;
private static List<KeyWordBean> lists = null;
// private static KeyWordBean bean = new KeyWordBean();
private static Map<Integer, List<KeyWordBean>> map = new HashMap<Integer, List<KeyWordBean>>();
public static Map<Integer, List<KeyWordBean>> getPDFText(String filePath) {
try {
PdfReader pdfReader = new PdfReader(filePath);
int pageNum = NumberOfPages();
PdfReaderContentParr pdfReaderContentParr = new PdfReaderContentParr(pdfReader);
公安制服
for (i = 1; i <= pageNum; i++) {
lists = wArrayList();
boundingRectange =new com.Rectangle2D.Float();
pdfReaderContentParr.processContent(i, new RenderListener() {
@Override
public void renderText(TextRenderInfo textRenderInfo) {
String text = Text(); // 整页内容
boundingRectange= Baline().getBoundingRectange();
KeyWordBean bean = new KeyWordBean();
bean.tX(boundingRectange.x);
bean.tY(boundingRectange.y);
bean.tPage(i);
bean.tText(text);
lists.add(bean);
}
@Override
public void renderImage(ImageRenderInfo arg0) {
// TODO Auto-generated method stub
}
@Override
public void endTextBlock() {
// TODO Auto-generated method stub
}
@Override
岳飞与秦桧
public void beginTextBlock() {
// TODO Auto-generated method stub
}
});
map.put(i, lists);
}
} catch (IOException e) {
e.printStackTrace();
}
环境保护心得体会return map;
}
public static void main(String[] args) {
Map<Integer, List<KeyWordBean>> keyWords = getPDFText("D:\\pdf\\src.pdf");
System.out.println(keyWords.size());
}
public static KeyWordBean getKeyWordXY(Map<Integer, List<KeyWordBean>> map2, int page, int num,String keyWord) { int keyMatch = 1;
StringBuilder content = new StringBuilder();
List<KeyWordBean> list = (page);
Collections.sort(list);//正序⽐较
StringBuilder builder = new StringBuilder();
for(int i=0; i<list.size(); i++){
KeyWordBean bean = (i);
String text = Text();
if(i+1 != list.size()){
KeyWordBean beanNext = (i+1);
float x = X()-X();
float y = Y()-Y();
if(y == 0 && x <= 1){
// System.out.print("去除因字体加粗产⽣的重复数字");
}el{
if (String(), keyWord) || ains(text, keyWord)) {
双椒鱼
if(keyMatch == num){
return bean;