java获取pdf坐标位置不对,怎么获取pdf文件中内容的末尾的位置

如何获取pdf文件中内容的末尾的位置?

我在web项目中将合同导出为pdf文件,同时想要在文件内容的末尾处添加一个公章,已经用Itext实现了导出和添加水印公章的功能,但是公章的位置是固定的。怎么才能将公章添加到合同的末尾处呢?

------解决思路----------------------

/**

*

* @Title: createWaterMarkSpread2PDF

* @Description: 针对PDF添加水印图片,并根据间隙计算,平铺  居中显示

* @param srcFilePath 源文件

* @param savePath 保存文件

* @param waterMarkPath 水印图片

* @param spaceX 横向间隙

* @param spaceY 纵向间隙

* @return

* @throws IOException

* @throws DocumentException

*/

public static void createWaterMarkSpread2PDF(String srcFilePath, String savePath, String waterMarkPath,float spaceX,float spaceY) throws IOException, DocumentException{

PdfReader pdfReader = null;

PdfStamper pdfStamper = null;

try{

pdfReader = new PdfReader(srcFilePath);

pdfStamper = new PdfStamper(pdfReader, new FileOutputStream(savePath));;

int pageCount = pdfReader.getNumberOfPages()+1;//获取PDF页数

Image image = Image.getInstance(waterMarkPath);

Rectangle size = pdfReader.getPageSize(1);

float pdfY = size.top();//PDF页面高度

float pdfX = size.right();//PDF页面宽度

Dimension dim = ImageUtil.getDim4Image(waterMarkPath);

PageSpreadSize pss = getPageSpreadSize(size, dim, spaceX, spaceY);

PdfContentByte content=null;

float heightWithSpace = (float)dim.getHeight()+(Float.isNaN(spaceY)

------解决思路----------------------

spaceY<=0?0:spaceY/2);

float widthWithSpace = (float)dim.getWidth()+(Float.isNaN(spaceX)

------解决思路----------------------

spaceX<=0?0:spaceX/2);

boolean isSpread = isImageSpread(pss);//是否平铺

//起始位置信息

float startX = isSpread?(pdfX-widthWithSpace*pss.getSizeX())/2:(float)(pdfX/2-dim.getWidth()/2);

float startY = isSpread?pdfY -(pdfY-heightWithSpace*pss.getSizeY())/2:((float)(pdfY/2-dim.getHeight()/2));

//给每页增加水印

for (int i = 1; i 

content = pdfStamper.getOverContent(i);

float posY = pdfY;//Y坐标绝对路径

if(!isSpread){

image.setAbsolutePosition(startX, startY);

content.addImage(image);

}

else{

//行

for(int pi=1; pi<=pss.getSizeY(); pi++){

posY = startY - pi*heightWithSpace;

float posX=0f; //X坐标绝对路径

//列

for(int py=1; py<=pss.getSizeX(); py++){

posX = startX+(py-1)*widthWithSpace;

image.setAbsolutePosition(posX, posY);

content.addImage(image);

}

}

}

content = null;//便于及时回收

}

}finally{

if(pdfStamper!=null){

pdfStamper.close();

pdfStamper = null;

pdfReader = null;

}

}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值