1.导入pom
<!--pdf-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-pdfa</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf.tool</groupId>
<artifactId>xmlworker</artifactId>
<version>5.5.12</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-xtra</artifactId>
<version>5.5.12</version>
<exclusions>
<exclusion>
<groupId>org.apache.commons</groupId>
<artifactId>commons-imaging</artifactId>
</exclusion>
</exclusions>
</dependency>
2.编写pdf填充实体类
public class PdfModelDTO {
//x坐标
private float xCoordinate;
//y坐标
private float yCoordinate;
//填充页码
private Integer pageNum;
//填充内容
private String content;
public PdfModelDTO(float xCoordinate, float yCoordinate, Integer pageNum, String content) {
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
this.pageNum = pageNum;
this.content = content;
}
public float getxCoordinate() {
return xCoordinate;
}
public void setxCoordinate(float xCoordinate) {
this.xCoordinate = xCoordinate;
}
public float getyCoordinate() {
return yCoordinate;
}
public void setyCoordinate(float yCoordinate) {
this.yCoordinate = yCoordinate;
}
public Integer getPageNum() {
return pageNum;
}
public void setPageNum(Integer pageNum) {
this.pageNum = pageNum;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "PdfModelDTO{" +
"xCoordinate=" + xCoordinate +
", yCoordinate=" + yCoordinate +
", pageNum=" + pageNum +
", content='" + content + '\'' +
'}';
}
}
3.编写pdf工具类
/** pdf内容填充
*
* @param oldFilePath 源文件地址
* @param newFilePath 新文件地址
* @param list 需要填充的信息
* @return 0:成功 1:源文件不是pdf文件 -9:异常
*/
public static int PdfTextFill(String oldFilePath, String newFilePath, List<PdfModelDTO> list){
try {
//判断源文件是否为pdf文件
String suffix = oldFilePath.substring(oldFilePath.lastIndexOf('.') + 1);
if (!"pdf".equalsIgnoreCase(suffix)) {
logger.info(oldFilePath+"文件不是pdf文件,无法进行内容填充");
return 1;
}
//文件解析
PdfReader reader = new PdfReader(oldFilePath);
FileOutputStream fOut = new FileOutputStream(newFilePath);
PdfStamper stp = new PdfStamper(reader, fOut);
// 获取文件总页数
int numberOfPages = reader.getNumberOfPages();
//内容填充
for (PdfModelDTO model : list) {
float xCoordinate = model.getxCoordinate();
float yCoordinate = model.getyCoordinate();
Integer pageNum = model.getPageNum();
String content = model.getContent();
//跳转到填充页并图层置顶
PdfContentByte pdfContentByte = stp.getOverContent(pageNum);
pdfContentByte.beginText();
//设置字体及字号
pdfContentByte.setFontAndSize(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false), 11);
//设置字体颜色
pdfContentByte.setColorFill(BaseColor.BLACK);
//设置填充的对齐方式、内容、位置和旋转
pdfContentByte.showTextAligned(Element.ALIGN_LEFT, content, xCoordinate, yCoordinate, 0);
pdfContentByte.endText();
}
//关闭资源连接
stp.close();
fOut.close();
reader.close();
logger.info("源文件" + oldFilePath + "内容填充完毕,新文件地址:" + newFilePath);
return 0;
} catch (Exception e){
logger.info("源文件" +oldFilePath+ "内容填充异常:" + e.getMessage());
return -9;
}
}
4.编写测试代码
public static void main(String[] args) {
//填充内容
List<PdfModelDTO> list = new ArrayList<>();
list.add(new PdfModelDTO(143f,675f,1,"1233211234567"));
list.add(new PdfModelDTO(341f,675f,1,"西山根"));
list.add(new PdfModelDTO(143f,653f,1,"某寡妇"));
list.add(new PdfModelDTO(310f,653f,1,"女"));
list.add(new PdfModelDTO(391f,491f,1,"2022年12月12日"));
//文件路径
String oleFilePath = "C:/Users/sky/Desktop/haha.pdf";
String newFilePath = "C:/Users/sky/Desktop/haha_"+System.currentTimeMillis()+".pdf";
//填充结果(0:成功 1:源文件不是pdf文件 -9:异常)
int result = PdfTextFill(oleFilePath, newFilePath,list);
}
注*:填充内容坐标位置需自行调试
5.运行测试
模板:
效果:
版权声明:本文为Y_haisong原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。