一、 准备模板
1.通过word生成文档
2.将word转为pdf模板
3.通过Adobe Acrobat DC ( 签名-》准备协议-》修改域设置对应字段别名)
二、 PDF生成主体代码
/**
* 客户告知书PDF合成操作
* @param signString
* @param path
* @param newPath
* @param imagePath
* @param customername
* @param idtype
* @param idNumer
* @param mobile
* @param mail
* @param address
* @param imageSeal
* @return
* @throws DocumentException
* @throws IOException
*/
public Map<String, Object> fillTemplate(String signString, String path, String newPath, String imagePath,String customername, String idtype, String idNumer, String mobile, String mail, String address, String imageSeal, int file_year,int file_month,int file_date) throws DocumentException, IOException {
//返回值定义
Map<String, Object> fileMap1 = new HashMap<>();
// 模板路径
String templatePath = path;
// 生成的新文件路径
String fileUUid1 = UUID.randomUUID().toString().replaceAll("-", "");
if (!signString.equals("")) {
fileUUid1 = signString + "@";
}
//文件
String fileName = customername + fileUUid1 + ".pdf";
String newPDFPath = newPath + fileName;
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
// 输出流
out = new FileOutputStream(newPDFPath);
// 读取pdf模板
reader = new PdfReader(templatePath);
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
//字体设置
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bf);
form.addSubstitutionFont(bf);
//方法三:使用资源字体(ClassPath)
/*BaseFont baseFont = BaseFont.createFont("/SIMYOU.TTF,1",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED); */
Map<String, Object> data = new HashMap<>();
data.put("Text1", customername);
data.put("Text2", idtype);
data.put("Text3", idNumer);
data.put("Text4", mobile);
data.put("Text5", mail);
data.put("Text6", address);
//时间
data.put("year", file_year);
data.put("month", file_month);
data.put("day", file_date);
// 7遍历data 给pdf表单表格赋值
for (String key : data.keySet()) {
form.setField(key, data.get(key).toString());
}
// 如果为false那么生成的PDF文件还能编辑,一定要设为true
stamper.setFormFlattening(true);
//合成图片-----------------------------------
int pageNo1 = form.getFieldPositions("Sign").get(0).page;
int pageNo2 = form.getFieldPositions("Seal").get(0).page;
Rectangle signRect1 = form.getFieldPositions("Sign").get(0).position;
Rectangle signRect2 = form.getFieldPositions("Seal").get(0).position;
float x1 = signRect1.getLeft();
float y1 = signRect1.getBottom();
float x2 = signRect2.getLeft();
float y2 = signRect2.getBottom();
// 读图片
Image image = Image.getInstance(imagePath);
Image image1 = Image.getInstance(imageSeal);
// 获取操作的页面
PdfContentByte under1 = stamper.getOverContent(pageNo1);
PdfContentByte under2 = stamper.getOverContent(pageNo2);
// 根据域的大小缩放图片
image.scaleToFit(signRect1.getWidth(), signRect1.getHeight());
image1.scaleToFit(signRect2.getWidth(), signRect2.getHeight());
// 添加图片
image.setAbsolutePosition(x1, y1);
image1.setAbsolutePosition(x2, y2);
under1.addImage(image);
under2.addImage(image1);
//-----------------------------
stamper.close();
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
//pdf模板总页数
int pageNum = reader.getNumberOfPages();
for (int i = 1; i <= pageNum; i++) {
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i);
copy.addPage(importPage);
}
doc.close();
//获取生成文件大小
File file = new File(newPDFPath);
long fileSize = file.length();
//封装返回值
fileMap1.put("FILENAME", fileName);
fileMap1.put("PATH", newPDFPath);
fileMap1.put("FILEID", fileUUid1);
fileMap1.put("SIZE", fileSize);
//返回值
return fileMap1;
}
三、入参解读
String signString = " 签名id";
String path = "pdf模板路径";
String newPath = "生成pdf存放路径";
String imageSeal = "印章图片路径";
String customername = "姓名";
String idtype = "证件类型";
String idNumer = "证件号";
String mobile = "手机号";
String mail = "邮箱";
String address = "地址";
//生成当前日期
Calendar cal = Calendar.getInstance();
//年
int file_year = cal.get(Calendar.YEAR);
//月
int file_month = cal.get(Calendar.MONTH)+1;
//日
int file_date = cal.get(Calendar.DATE);