java之获取pdf文字坐标并添加印章

Shaka 6月前 ⋅ 327 阅读

在网上查了很多资料,都不是很全,但是基本满足需求.

我做了总结与修改,已经全部解决.

这个需求需要解决的问题:

1.获取文字坐标位置.

2.替换PDF文字

3.添加图片,设置图片宽度覆盖文字.

下面贴出代码,我就不说了,多加点注释自己看.

static float[] position;
static Integer num;
@Test
public void wordToPdf(){
//源文件
String inputFile = "C:\\Users\\Desktop\\1.pdf";
File file = new File(inputFile);
//目标文件
String outputFile = inputFile.replace(file.getName(),"temp"+file.getName());
try {
//获取读取器读取pdf
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
outputFile));
//读取页数
int pageNum = reader.getNumberOfPages();
PdfReaderContentParser pdfReaderContentParser = new PdfReaderContentParser(reader);
for(int i = 1;i <= pageNum;i++){
num = i;
//进行内容解析
pdfReaderContentParser.processContent(i,getRenderListener(stamper));
}
//切记关闭流
stamper.close();
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
//删除源文件,重命名输出文件名字(这里我自己也不喜欢感觉很傻)
new File(inputFile).delete();
new File(outputFile).renameTo(new File(inputFile));
}
private RenderListener getRenderListener(final PdfStamper stamper) {
return new RenderListener(){
//图片路径
public void renderText(TextRenderInfo textRenderInfo) {
String picPath;
//获取片段
String text = textRenderInfo.getText();
//签字/日期/印章
if (null != text && text.contains("签字")) {
picPath = "d:\\签名\\签名.jpg";
//获取文字坐标
getFontPosition(textRenderInfo,stamper,picPath,4);
} else if (null != text && text.contains("公章")) {
picPath = "d:\\签名\\天龙宝印.png";
//获取文字坐标
getFontPosition(textRenderInfo,stamper,picPath,8);
}

}
public void beginTextBlock() {}
public void endTextBlock() {}
public void renderImage(ImageRenderInfo renderInfo) {}
};
//获取文字坐标 (multiple=倍数,我想让印章大一些)
private void getFontPosition(TextRenderInfo textRenderInfo,PdfStamper stamper,String picPath,int multiple){
RectangularShape rectBase = textRenderInfo.getBaseline().getBoundingRectange();
//获取文字下面的矩形
Rectangle2D.Float rectAscen = textRenderInfo.getAscentLine().getBoundingRectange();
//计算出文字的边框矩形
float leftX = (float) rectBase.getMinX();
float leftY = (float) rectBase.getMinY() - 1;
float rightX = (float) rectAscen.getMaxX();
float rightY = (float) rectAscen.getMaxY() + 1;
Rectangle2D.Float rect = new Rectangle2D.Float(leftX, leftY, rightX - leftX, rightY - leftY);
System.out.println("x:" + rect.x + "--y:" + rect.y + "--width:" + rect.width + "--height:" + rect.height);
position = new float[4];
position[0] = rect.x;
position[1] = rect.y;
position[2] = (float) rect.getWidth();
position[3] = (float) rect.getHeight();
//覆盖匹配文字
replaceText(stamper);
//添加签名印章
addImg(stamper,picPath,multiple);
}
//覆盖匹配文字
private void replaceText(PdfStamper stamper){
PdfContentByte canvas = stamper.getOverContent(1);
canvas.saveState();
canvas.setColorFill(BaseColor.WHITE);
canvas.rectangle(position[0],position[1],position[2],position[3]);
canvas.fill();
canvas.restoreState();
}
//添加图片/传值不同图片不同
private void addImg(PdfStamper stamper,String picPath,int multiple){
try {
//获取图片
Image image = Image.getInstance(picPath);
//设置图片大小
image.scaleToFit(position[2]*multiple,position[3]*multiple);
//设置图片位置
image.setAbsolutePosition(position[0]-position[2],position[1]-position[3]*2);
//设置至于顶层,底层UnderContent
PdfContentByte under = stamper.getOverContent(num);
//添加图片
under.addImage(image);
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}

注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: