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);
//獲取操作的頁面
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ù)器上可能存在字體不存在的問題,可以在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)文章
SpringBoot整合Javamail實(shí)現(xiàn)郵件發(fā)送功能
郵件發(fā)送是一個(gè)很普遍的功能,springboot整合了相關(guān)的starter,本文給大家介紹了可以實(shí)現(xiàn)一個(gè)簡單的郵件發(fā)送功能的實(shí)例,文中通過代碼給大家介紹的非常詳細(xì),感興趣的朋友可以參考下2023-12-12
Java如何通過"枚舉的枚舉"表示二級(jí)分類的業(yè)務(wù)場(chǎng)景
這篇文章主要介紹了Java如何通過"枚舉的枚舉"表示二級(jí)分類的業(yè)務(wù)場(chǎng)景問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06
Java中Map實(shí)現(xiàn)線程安全的3種方式
本文主要介紹了Java中Map實(shí)現(xiàn)線程安全的3種方式,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
SpringBoot中ResponseEntity的使用方法舉例詳解
ResponseEntity是Spring 的一個(gè)用于表示 HTTP 響應(yīng)的全功能對(duì)象,它可以包含響應(yīng)的狀態(tài)碼、頭信息及響應(yīng)體內(nèi)容,下面這篇文章主要介紹了SpringBoot中ResponseEntity使用方法的相關(guān)資料,需要的朋友可以參考下2025-09-09
使用MyBatis攔截器實(shí)現(xiàn)sql查詢權(quán)限動(dòng)態(tài)修改代碼實(shí)例
這篇文章主要介紹了使用MyBatis攔截器實(shí)現(xiàn)sql查詢權(quán)限動(dòng)態(tài)修改代碼實(shí)例,為了不耦合,現(xiàn)在的方案是在需要鑒權(quán)的Mybatis?Mapper方法上增加一個(gè)注解,在運(yùn)行過程中判斷該注解存在即對(duì)sql進(jìn)行修改,需要的朋友可以參考下2023-08-08

