当前位置: 首页 > news >正文

SpringBoot对PDF进行模板内容填充、电子签名合并

1. 依赖引入–这里只包含额外引入的包 原有项目包不含括在内

<!--		pdf编辑相关-->
<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.13.3</version>
</dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version>
</dependency>

2. pdf模板建立

  • 首先需要准备一个可编辑的pdf文件,文件中要包含表单内容
  • 为表单建立域(这里以Adobe Acrobat DC为例)
  • 找到工具栏的准备表单,然后点击,如下图所示

image.png

  • 针对域设置字段名

image.png

  • 另存为此时文件就是一个pdf填充模板,后面代码实现可以直接使用
    3. 代码实现–工具类可以直接使用(注意修改文件位置,需要提前准备好模板)

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import org.apache.commons.lang3.StringUtils;import java.io.*;
import java.util.HashMap;
import java.util.Map;/*** @desc: pdf编辑相关工具类* @author: lc* @since: 2023/12/13*/
public class PdfUtils {/*** 利用模板生成pdf保存到某路径下*/public static void pdfOut(Map<String, Object> map) throws IOException, DocumentException {// 模板路径String templatePath = (String) map.get("templatePath");//新的pdf文件String newPdfPath = (String) map.get("newPdfPath");//签字图片的地址String signPath = (String) map.get("signPath");File file1 = new File(newPdfPath);if (!file1.exists()) {try {file1.createNewFile();} catch (IOException e) {e.printStackTrace();}}//pdf模板文件InputStream input = new FileInputStream(templatePath);//生成的新文件File file = new File(newPdfPath);FileOutputStream fos = new FileOutputStream(file);PdfReader reader = new PdfReader(input);PdfStamper stamper = new PdfStamper(reader, fos);// 提取PDF中的表单AcroFields form = stamper.getAcroFields();// 设置中文字体String prefixFont = "";String os = System.getProperties().getProperty("os.name");if (os.startsWith("win") || os.startsWith("Win")) {prefixFont = "C:\\Windows\\Fonts" + File.separator;} else {prefixFont = "/usr/share/fonts/chinese" + File.separator;}BaseFont baseFont = BaseFont.createFont(prefixFont + "simsun.ttc,0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);form.addSubstitutionFont(baseFont);//文字类的内容处理Map<String, String> datemap = (Map<String, String>) map.get("dateMap");//填充值for (String key : datemap.keySet()) {String value = datemap.get(key);//设置字体大小form.setFieldProperty(key, "textsize", 12f, null);form.setField(key, value);}//签名图片路径存在才进行图片合并if (StringUtils.isNotBlank(signPath)){//进行签字的填充int pageNo = form.getFieldPositions("sign").get(0).page;Rectangle signRect = form.getFieldPositions("sign").get(0).position;float x = signRect.getLeft();float y = signRect.getBottom();//读取图片Image image = Image.getInstance(signPath);//获取操作的页面PdfContentByte content = stamper.getOverContent(pageNo);// 根据域的大小缩放图片image.scaleToFit(signRect.getWidth(), signRect.getHeight());// 添加图片image.setAbsolutePosition(x, y);content.addImage(image);fos.flush();}// 生成PDFstamper.setFormFlattening(true);stamper.close();reader.close();fos.close();input.close();}public static void main(String[] args) throws DocumentException, IOException {Map<String, Object> params = new HashMap<>();//测试数据HashMap<String, Object> map = new HashMap<>();map.put("username","测试号");map.put("name1","张三");map.put("name2","李四");map.put("sex","男");map.put("address","8号楼");//需要赋值的模板内容params.put("dateMap",map);//模板位置String templatePath = "C:\\Users\\lc\\Desktop\\test.pdf";//新生成pdf文件位置String newPdfPath = "C:\\Users\\lc\\Desktop\\test1.pdf";//签名图片位置String signPath = "C:\\Users\\lc\\Desktop\\qs.png";params.put("templatePath",templatePath);params.put("newPdfPath",newPdfPath);params.put("signPath",signPath);pdfOut(params);}}

部署到linux服务器上可能存在字体不存在的问题,可以在linux下载对应的字体或者将window中的字体拷贝到linux上,这里就不详述了可以自行百度

http://www.lryc.cn/news/261611.html

相关文章:

  • Vue3快速上手笔记
  • LLM中的Prompt提示
  • 【算法Hot100系列】最长回文子串
  • KaiwuDB × 国网山东综能 | 分布式储能云边端一体化项目建设
  • elasticsearch查询出现Limit of total fields 1000 has been exceeded
  • TCP/IP详解——DHCP 协议
  • 牛客后端开发面试题3
  • Postman-脚本自动化及定时执行脚本(7)
  • 基于SSM的影视企业全渠道会员管理系统(有报告)。Javaee项目
  • 【C++】 C++11 新特性探索:decltype 和 auto
  • 【Jeecg Boot 3 - 第二天】1.2、jar 包和 lib 依赖分离,部署包缩小100倍
  • 电商平台的易聊集成:无代码开发,API连接,CRM支持
  • Draw.io or diagrams.net 使用方法
  • CAPL——发送自定义报文
  • 接口自动化测试实操【设置断言思路】
  • windows redis 允许远程访问配置
  • 三、JS逆向
  • HBase 高可用集群详细图文安装部署
  • 现代雷达车载应用——第2章 汽车雷达系统原理 2.6节 雷达设计考虑
  • 【JVM从入门到实战】(五)类加载器
  • 计算机毕业设计 基于Web的城市旅游网站的设计与实现 Java实战项目 附源码+文档+视频讲解
  • 【人工智能革命】:AIGC时代的到来 | 探索AI生成内容的未来
  • spring-boot-data-jpa、JPA实现分页
  • 云原生之深入解析如何在Kubernetes中快速启用Cgroup V2支持
  • QT实现四则运算计算器
  • mysql的redolog、undo、binlog的作用
  • 京东大数据-10月京东咖啡机市场销售数据分析-销售额增长41%,德龙等海外头部品牌店铺数据分析
  • 【Android12】WindowManagerService架构分析
  • 部署LVS的NET模式
  • 如何在Facebook Business Manager进行企业认证