SpringBoot對(duì)PDF進(jìn)行模板內(nèi)容填充與電子簽名合并詳解
1. 依賴引入
這里只包含額外引入的包 原有項(xiàng)目包不含括在內(nèi)
<!-- pdf編輯相關(guān)--> <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模板建立
首先需要準(zhǔn)備一個(gè)可編輯的pdf文件,文件中要包含表單內(nèi)容
為表單建立域(這里以Adobe Acrobat DC為例)
找到工具欄的準(zhǔn)備表單,然后點(diǎn)擊,如下圖所示

針對(duì)域設(shè)置字段名

另存為此時(shí)文件就是一個(gè)pdf填充模板,后面代碼實(shí)現(xiàn)可以直接使用
3. 代碼實(shí)現(xiàn)
工具類可以直接使用(注意修改文件位置,需要提前準(zhǔn)備好模板)
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編輯相關(guān)工具類
* @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();
// 設(shè)置中文字體
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);
//文字類的內(nèi)容處理
Map<String, String> datemap = (Map<String, String>) map.get("dateMap");
//填充值
for (String key : datemap.keySet()) {
String value = datemap.get(key);
//設(shè)置字體大小
form.setFieldProperty(key, "textsize", 12f, null);
form.setField(key, value);
}
//簽名圖片路徑存在才進(jìn)行圖片合并
if (StringUtils.isNotBlank(signPath)){
//進(jìn)行簽字的填充
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);
//獲取操作的頁(yè)面
PdfContentByte content = stamper.getOverContent(pageNo);
// 根據(jù)域的大小縮放圖片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加圖片
image.setAbsolutePosition(x, y);
content.addImage(image);
fos.flush();
}
// 生成PDF
stamper.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<>();
//測(cè)試數(shù)據(jù)
HashMap<String, Object> map = new HashMap<>();
map.put("username","測(cè)試號(hào)");
map.put("name1","張三");
map.put("name2","李四");
map.put("sex","男");
map.put("address","8號(hào)樓");
//需要賦值的模板內(nèi)容
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服務(wù)器上可能存在字體不存在的問(wèn)題,可以在linux下載對(duì)應(yīng)的字體或者將window中的字體拷貝到linux上,這里就不詳述了
以上就是SpringBoot對(duì)PDF進(jìn)行模板內(nèi)容填充與電子簽名合并詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot PDF模板內(nèi)容填充的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Java關(guān)于JDK中時(shí)間日期的API
這篇文章主要介紹了詳解Java關(guān)于JDK中時(shí)間日期的API,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
java異步調(diào)用的4種實(shí)現(xiàn)方法
日常開發(fā)中,會(huì)經(jīng)常遇到說(shuō),前臺(tái)調(diào)服務(wù),本文主要介紹了java異步調(diào)用的4種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
在SpringBoot中使用@Value注解來(lái)設(shè)置默認(rèn)值的方法
Spring Boot提供了一種使用注解設(shè)置默認(rèn)值的方式,即使用 @Value 注解,下面這篇文章主要給大家介紹了關(guān)于如何在SpringBoot中使用@Value注解來(lái)設(shè)置默認(rèn)值的相關(guān)資料,需要的朋友可以參考下2023-10-10
Java C++題解leetcode816模糊坐標(biāo)示例
這篇文章主要為大家介紹了Java C++題解leetcode816模糊坐標(biāo)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
springboot 集成redis哨兵主從的實(shí)現(xiàn)
本文主要介紹了springboot 集成redis哨兵主從的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07

