Java 修改 PDF 图片,用于生成荣誉证书

1、需要准备一张荣誉证书,并且使用 Adobe Acrobat 打开,再点击 :文件>>创建>>创建表单。Adobe Acrobat软件则会自动定位到动态表单项,需要在动态表单项中设置变量,这里我们设置两个变量:data、time.

2、参考一下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
public TableDataInfo getCertificateImage(ResourceFile resourceFile) {
TableDataInfo tableDataInfo = new TableDataInfo();
PageData pd = getPageData();

try {
//查询使用的模版详情,这里我们先将证书模版提前存储到服务器 ResourceFile 为文件存储表的实体类
List<ResourceFile> resourceFiles = resourceFileService.list(new QueryWrapper<ResourceFile>().eq("id", resourceFile.getId()));
//模版地址
String oldFilePath = resourceFiles.get(0).getResourcePath() + resourceFiles.get(0).getResourceFileName();
//定义新证书文件名称
String fileName = "/" +UUID.randomUUID() + ".pdf";
//定义新证书地址
String resourcePath = "/certificate" + getFilePath();
String newPDFPath = linuxPath + resourcePath + "/" + fileName;

PdfReader reader = new PdfReader(oldFilePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();

/* 2.读取PDF模板内容 */
PdfStamper ps = new PdfStamper(reader, bos);
PdfContentByte under = ps.getUnderContent(1);
System.out.println("采用PDF模板:" + oldFilePath);

/* 3.法1:设置使用itext-asian.jar的中文字体 */
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

/*法2:使用项目下的自定义的中文字体
bfChinese = BaseFont.createFont("static/font/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);*/

// /*法3:使用windows系统下的字体库
// BaseFont bfChinese = BaseFont.createFont("c://windows//fonts//三极空叠体-REGULAR.TTF,1",BaseFont.IDENTITY_H, false);

ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
fontList.add(bf);

/* 4.获取模板中的所有字段 */
AcroFields fields = ps.getAcroFields();
fields.setSubstitutionFonts(fontList);

//调用方法执行写入 data() 为证书pdf表单中的字段
fileData(fields, data());

/* 必须要调用这个,否则文档不会生成的 */
ps.setFormFlattening(true);
ps.close();

/* 5.将要生成的目标PDF文件名称 */
OutputStream fos = new FileOutputStream(newPDFPath);
fos.write(bos.toByteArray());
System.out.println("新证书已生成:" + newPDFPath);

//存储证书到数据库
resourceFile.setCreateId(getUserId().intValue());
resourceFile.setCreator(getLoginUser().getUser().getNickName());
resourceFile.setCreateTime(new Date());
resourceFile.setResourcePath(resourcePath + "/");
resourceFile.setResourceFileName(fileName);
resourceFile.setRealFileName(pd.getString("testPeople") + "的证书");
resourceFileService.save(resourceFile);

fos.flush();
fos.close();
bos.close();

PageData pdd = new PageData();
List<PageData> list = new ArrayList();
pdd.put("resourceFileId", resourceFile.getId());
pdd.put("creator", getLoginUser().getUser().getNickName());
pdd.put("fileName", fileName);
pdd.put("realFileName", pd.getString("testPeople") + "的证书");
pdd.put("fileUrl", path + resourcePath + "/" + fileName);
tableDataInfo.setMsg("下载证书中,请耐心等待");
list.add(pdd);
tableDataInfo.setRows(list);
tableDataInfo.setCode(HttpStatus.SUCCESS);
return tableDataInfo;
} catch (Exception e) {
tableDataInfo.setMsg("下载证书失败,请联系管理人员");
tableDataInfo.setCode(HttpStatus.ERROR);
return tableDataInfo;
}



// 获取pdf模板中有哪些字段key+赋值的值value
public static void fileData(AcroFields fields, Map<String, String> data) throws IOException, DocumentException {
for (String key : data.keySet()) {
String value = data.get(key); // 调用data方法获取值
System.out.println(key + "字段:" + value);
fields.setField(key, value); // 为字段赋值,注意字段名称是区分大小写的
}
}

// 为需要填入的数据value赋值
public static Map<String, String> data() {
Map<String, String> data = new HashMap<String, String>();
data.put("name", "最帅"); // 字段需要对应pdf模板里面的名称
data.put("time", "天荒地老");
return data;
}

/**
* 获取年/月/日目录结构
*
* @return String
*/
public String getFilePath() {
String path = DateUtilsTwo.getDay();
return "/" + path.replaceAll("-", "");
}

Java 修改 PDF 图片,用于生成荣誉证书
https://tdsgpo.top/2022/09/01/Java 修改 PDF 图片,用于生成荣誉证书/
作者
DDS
发布于
2022年9月1日
许可协议