导入Maven pom依赖
<!-- pdf样式 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
<!--pdf相关操作-->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.3</version>
</dependency>
编辑pdf,写入编辑表单域
package com.ruoyi;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import com.ruoyi.common.config.Global;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.HashMap;
import java.util.Map;
/**
* Created with IntelliJ IDEA.
*
* @Auther: ljt
* @Version 1.0
* @Date: 2020/06/05/16:18
* @Description:
*/
public class CreatePDFUtil {
public static void main(String[] args) {
//pdf模板路径
String templatePdf = "d:\\社区证明_20200605163523.pdf";
//生成新写入数据pdf路径
String newPdfPath = "d:\\new社区证明_20200605163523.pdf";
//自定义字体路径
String fontPath = "";
Map<String,String> map = convertMap();
getBillByPdf(templatePdf,newPdfPath,fontPath,map);
}
public static Map<String, String> convertMap() {
Map<String, String> map = new HashMap<>();
map.put("year", "2020");
map.put("month", "10");
map.put("day", "24");
map.put("userName", "李某人");
map.put("singleCommunity","1024");
map.put("sex","未知");
map.put("nation","1024");
map.put("birthday","1024");
map.put("number","1024");
map.put("address","1024地区");
map.put("singleCommunity2","1024");
map.put("png", "d://201411281904190406.png");
return map;
}
/**
*
* @param templatePdf 模板路径
* @param newPdfPath 新pdf路径
* @param fontPath 字体路径
* @param map 参数
*/
public static void getBillByPdf(String templatePdf ,String newPdfPath, String fontPath,Map<String,String> map) {
PdfReader reader = null;
FileOutputStream out = null;
ByteArrayOutputStream bos = null;
PdfStamper stamper = null;
File file = new File(Global.getUploadPath());
if (!file.exists() && !file.isDirectory()) {
file.mkdir();
}
try {
out = new FileOutputStream(newPdfPath);
reader = new PdfReader(templatePdf);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//注意:加入字体输出的模板太大,可以用下面这个不带字体的
BaseFont bfChinese = null;
if("".equals(fontPath)){
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
}else{
//导入指定字体,
bfChinese = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H,
BaseFont.NOT_EMBEDDED);
}
Map<String, AcroFields.Item> fieldMap = form.getFields();
for (Map.Entry<String, AcroFields.Item> entry : fieldMap.entrySet()) {
String name = entry.getKey();
if("image".equals(name)){
int pageNo = form.getFieldPositions(name).get(0).page;
Rectangle signRect = form.getFieldPositions(name).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
// 读图片
Image image = Image.getInstance(map.get(name));
// 获取操作的页面
PdfContentByte under = stamper.getOverContent(pageNo);
// 根据域的大小缩放图片
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// 添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
}else{
form.setField(name, map.get(name));
}
}
form.addSubstitutionFont(bfChinese);
form.setFieldProperty("content", "textfont", bfChinese, null);
//true代表生成的PDF文件不可编辑
stamper.setFormFlattening(true);
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}