|
|
@ -375,6 +375,76 @@ public class ContractServiceImpl implements ContractService { |
|
|
|
return Result.OK("pdf地址", pdfUrl); |
|
|
|
} |
|
|
|
|
|
|
|
public Result<?> signPdf(String pdfPath, String imagePath, int pageNo, float positionX, float positonY, float imageWidth, float imageHeight){ |
|
|
|
String tempPath = System.getProperty("java.io.tmpdir") + File.separator + "resultPdf.pdf";//临时文件路径 |
|
|
|
String resultPdfPath = null; |
|
|
|
|
|
|
|
try{ |
|
|
|
//1、获得文件流对象 |
|
|
|
InputStream pdfInputStream = new URL(pdfPath).openStream(); |
|
|
|
InputStream imageInputStream = new URL(imagePath).openStream(); |
|
|
|
//2、得到PDF文档对象 |
|
|
|
PDDocument document = PDDocument.load(pdfInputStream); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int pageNum = document.getNumberOfPages(); |
|
|
|
if(pageNo<0 || pageNo>pageNum){ |
|
|
|
String message = "签名页码超出范围(0~"+pageNum+"),请输入正确的签名页码,当前传入页码:"+pageNo; |
|
|
|
return Result.OK("签名页码超出范围,输入正确的签名页码"); |
|
|
|
} |
|
|
|
//3、由Document得到Page对象 |
|
|
|
PDPage page = document.getPage(pageNo-1);//页码下标从0开始 |
|
|
|
//4、通过图片路径和PDF文档对象得到图片image对象 |
|
|
|
//PDImageXObject image = PDImageXObject.createFromFile(PNG_PATH, document); |
|
|
|
PDImageXObject image = createImageFromInputStream(document, imageInputStream); |
|
|
|
//5、创建pageStream对象 |
|
|
|
PDPageContentStream pageStream = new PDPageContentStream(document, page,PDPageContentStream.AppendMode.APPEND,false,false); |
|
|
|
|
|
|
|
/** |
|
|
|
* 可优化位置 |
|
|
|
*/ |
|
|
|
//6、pageStream对象绘制图片位置及大小,已PDF文件右下角为原点(x,y)是图片左下角左边,width、height是图片的长和宽 |
|
|
|
//pageStream.drawImage(image, 450, 700,50,50); |
|
|
|
|
|
|
|
//获取pdf宽度 |
|
|
|
float width = document.getPage(pageNo).getMediaBox().getWidth(); |
|
|
|
positionX = (float) (width * positionX / 750.0); |
|
|
|
positonY = (float) (width * positonY / 750.0); |
|
|
|
imageWidth = (float) (width * imageWidth / 750.0); |
|
|
|
imageHeight = (float) (width * imageHeight / 750.0); |
|
|
|
|
|
|
|
pageStream.drawImage(image, positionX, positonY,imageWidth,imageHeight); |
|
|
|
pageStream.close(); |
|
|
|
//7、保存PDF到指定路径 |
|
|
|
document.save(tempPath); |
|
|
|
|
|
|
|
//8、将输出路径转换成 File,后续进行 OSS 上传 |
|
|
|
File file = new File(tempPath); |
|
|
|
InputStream inputStream = new FileInputStream(file); |
|
|
|
//获取当前时间 |
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); |
|
|
|
Date now = new Date(); |
|
|
|
String formattedDate = sdf.format(now); |
|
|
|
resultPdfPath = OssBootUtil.upload(inputStream, formattedDate+".pdf"); |
|
|
|
|
|
|
|
//删除临时文件 |
|
|
|
file.delete(); |
|
|
|
|
|
|
|
//8、关闭流 |
|
|
|
document.close(); |
|
|
|
pdfInputStream.close(); |
|
|
|
|
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
return Result.OK("pdf添加签名失败!"); |
|
|
|
} |
|
|
|
|
|
|
|
return Result.OK("pdfPath", resultPdfPath); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/***************************************************************************/ |
|
|
|
/** |
|
|
|
* 将 pdf链接文件转换成多图片base64 |
|
|
|
* @param pdfLink |
|
|
|