首先下载Adobe Acrobat DC工具:
链接: https://pan.baidu.com/s/18iWctdrF0Nz07i8in6cf0A 提取码: syju
通过工具打开pdf母版,点击创建表单,然后插入文本,文本变量就是我们java代码中需要插入的值,文本域可以设置字体,颜色大小等信息
import cn.hutool.core.util.RandomUtil;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;
/**
* @ClassName PdfUtils
* @Description TODO
* @Author 田剑
* @Date 2021/4/13 11:26
*/
@Slf4j
public class PdfUtils {
// 利用模板生成pdf
public static File pdfout(Map<String,Object> o,String templatePath) {
// 模板路径
// 生成的新文件路径
File upload= null;
try {
upload = File.createTempFile(RandomUtil.randomNumbers(16),".pdf");
} catch (IOException e) {
log.error("创建临时文件失败",e);
}
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
try {
BaseFont bf = BaseFont.createFont();
Font FontChinese = new Font(bf, 5, Font.NORMAL);
out = new FileOutputStream(upload);// 输出流
reader = new PdfReader(templatePath);// 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//文字类的内容处理
Map<String,String> datemap = (Map<String,String>)o.get("datemap");
form.addSubstitutionFont(bf);
for(String key : datemap.keySet()){
String value = datemap.get(key);
form.setField(key,value);
}
//图片类的内容处理
// Map<String,String> imgmap = (Map<String,String>)o.get("imgmap");
// for(String key : imgmap.keySet()) {
// String value = imgmap.get(key);
// String imgpath = value;
// int pageNo = form.getFieldPositions(key).get(0).page;
// Rectangle signRect = form.getFieldPositions(key).get(0).position;
// float x = signRect.getLeft();
// float y = signRect.getBottom();
// //根据路径读取图片
// Image image = Image.getInstance(imgpath);
// //获取图片页面
// PdfContentByte under = stamper.getOverContent(pageNo);
// //图片大小自适应
// image.scaleToFit(signRect.getWidth(), signRect.getHeight());
// //添加图片
// image.setAbsolutePosition(x, y);
// under.addImage(image);
// }
stamper.setFormFlattening(true);// 如果为false,生成的PDF文件可以编辑,如果为true,生成的PDF文件不可以编辑
stamper.close();
Document doc = new Document();
Font font = new Font(bf, 32);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
} catch (IOException e) {
log.error("IOException",e);
} catch (DocumentException e) {
log.error("DocumentException",e);
}
return upload;
}
//
// public static void main(String[] args) {
// Map<String,String> map = new HashMap();
// map.put("courseDate","07 September 2020");
// map.put("printName","AU Bok Toh "+RandomUtil.randomString(6));
// map.put("subTitle","Orthopaedic Trainees");
// map.put("certNo","Serial Number: 2019/2020-00154");
//
//// Map<String,String> map2 = new HashMap();
//// map2.put("img","c:/50336.jpg");
//
// Map<String,Object> o=new HashMap();
// String modelUrl="G:/qehconnect/service/src/main/resources/template/haanec.pdf";
// o.put("datemap",map);
//// o.put("imgmap",map2);
// File f=pdfout(o,modelUrl);
// log.info(f.getAbsolutePath());
// }
}
调用如下:
Map<String,String> map = new HashMap(); map.put("courseDate",jo.getString("certDate")); map.put("printName",jo.getString("printedName")); map.put("subTitle",jo.getString("subTitle")); map.put("certNo","Serial Number:"+jo.getString("certNo")); Map<String,Object> o=new HashMap(); o.put("datemap",map); File pdfFile=PdfUtils.pdfout(o,path);