springboot itextpdf 形式导出pdf
- 先看效果(这里只设置了软件版本和 完成情况的勾选框)
- 导入pom依赖
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--itextpdf-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
- 功能实现
package com.example.demotest.controller;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
@RestController
public class PdfController {
/**
* 导出pdf
*
* @param response
* @return
* @author shaolin
*/
@PostMapping(value = {"/exportpdf"})
public String exportPdf(HttpServletResponse response) throws IOException {
// 指定解析器
System.setProperty("javax.xml.parsers.DocumentBuilderFactory",
"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
String filename = "pdf/计算机编号.pdf";
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;fileName="
+ URLEncoder.encode(filename, StandardCharsets.UTF_8));
OutputStream os = null;
PdfStamper ps = null;
PdfReader reader = null;
try {
os = response.getOutputStream();
// 2 读入pdf表单
Path path = Paths.get("pdf", "计算机编号.pdf");
String pathing = path.toAbsolutePath().toString();
// 输出文件路径
reader = new PdfReader(pathing);
// 3 根据表单生成一个新的pdf
ps = new PdfStamper(reader, os);
// 4 获取pdf表单
AcroFields form = ps.getAcroFields();
// 5给表单添加中文字体 这里采用系统字体。不设置的话,中文可能无法显示
BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1",
BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
// BaseFont bf = BaseFont.createFont("SimSun.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
form.addSubstitutionFont(bf);
// 6查询数据================================================
Map<String, Object> data = new HashMap<String, Object>();
data.put("deptName", "测试部门");
data.put("version", "1");
// 7遍历data 给pdf表单表格赋值
for (String key : data.keySet()) {
form.setField(key, data.get(key).toString());
}
// 多选框设置, name是名称, value 是导出值
ps.setFormFlattening(true);
form.setField("test", "On", true);
// -----------------------------pdf 添加图片----------------------------------
// 通过域名获取所在页和坐标,左下角为起点
System.out.println("pdf 添加图片");
// String imgpath = "e:/美女.png";
// int pageNo = form.getFieldPositions("img").get(0).page;
// Rectangle signRect = form.getFieldPositions("img").get(0).position;
// float x = signRect.getLeft();
// float y = signRect.getBottom();
// // 读图片
// Image image = Image.getInstance(imgpath);
// // 获取操作的页面
// PdfContentByte under = ps.getOverContent(pageNo);
// // 根据域的大小缩放图片
// image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// // 添加图片
// image.setAbsolutePosition(x, y);
// under.addImage(image);
} catch (DocumentException e) {
throw new RuntimeException(e);
} finally {
try {
ps.close();
reader.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
-
对应项目路径截图
-
模板设置,这里使用的是adobe2024版
4.1 对于数字和文字的设置为文本域
4.2 双击截图里的文本域,出现下图所示,名称,这个名称就是你的字段名称
如果要显示中文,在外观这里设置宋体, 不然导出pdf 文字不显示
- 单选框功能实现
5.1 选择复选框
5.2 设置字段名,以及字段值和展示的形式(例如框内是对勾,还是个黑点,等等)
对应代码里的(这里要设为true才行,不然框里也不显示对勾)
form.setField(“test”, “On”, true);