##合成pdf工具类
String templatePath = "";
String fileName = "";
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
ByteArrayOutputStream byteArrayOutputStream = null;
String fornt = "https://yapp-image.oss-cn-shanghai.aliyuncs.com/customer/templateContract/f545c5c348ee419e81e7919c799fce6b.ttf";
try {
BaseFont font = BaseFont.createFont(fornt, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//读取pdf模板表单
pdfReader = new PdfReader(templatePath);
//字节数组流,用来缓存文件流
byteArrayOutputStream = new ByteArrayOutputStream();
//根据模板表单生成一个新的pdf
pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream);
//获取新的pdf表单
AcroFields form = pdfStamper.getAcroFields();
form.addSubstitutionFont(font);
for (String key : map.keySet()) {
//此处需要跳过,不跳过,会导致图片的路径会写到图片上面
if ("image".equals(key)) {
continue;
}
//设置普通文本数据
form.setField(key, map.get(key));
}
//图片特殊处理
if (null != map.get("image")) {
//通过域名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions("image").get(0).page;
Rectangle signRect = form.getFieldPositions("image").get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
String img = map.get("image");
//根据路径或url读取图片
Image image = Image.getInstance(img);
//获取图片页面
PdfContentByte under = pdfStamper.getOverContent(pageNo);
//图片大小自适应
image.scaleAbsolute(signRect.getWidth(), signRect.getHeight());
image.setAbsolutePosition(x, y);
under.addImage(image);
}
pdfStamper.setFormFlattening(true);
pdfStamper.close();
pdfReader.close();
Document document = new Document();
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
PdfCopy pdfCopy = new PdfCopy(document, fileOutputStream);
document.open();
PdfImportedPage importedPage = pdfCopy.getImportedPage(new PdfReader(byteArrayOutputStream.toByteArray()), 1);
pdfCopy.addPage(importedPage);
document.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (pdfStamper != null) {
pdfStamper.close();
}
if (pdfReader != null) {
pdfReader.close();
}
}
public static void signContractTwo(Map<String, Object> data, String fontLoc, URL templateUrl, ByteArrayOutputStream byteArrayOutputStream) throws Exception {
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try {
BaseFont baseFont = BaseFont.createFont(fontLoc, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font font = new Font(baseFont, 10, Font.BOLD);
pdfReader = new PdfReader(templateUrl);
pdfStamper = new PdfStamper(pdfReader, byteArrayOutputStream);
AcroFields form = pdfStamper.getAcroFields();
for (String key : data.keySet()) {
//此处需要跳过,不跳过,会导致图片的路径会写到图片上面
if (StringUtils.equals("image", key)) {
continue;
}
String value = data.get(key).toString();
if (StringUtils.equals("time", key)) {
form.addSubstitutionFont(baseFont);
//设置普通文本数据
form.setField(key, value);
continue;
}
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
PdfContentByte ctOver = pdfStamper.getOverContent(pageNo);
ColumnText ct = new ColumnText(ctOver);
ct.setLeading(0, 1);
ct.setAlignment(Element.ALIGN_LEFT);
ct.setText(new Paragraph(value, font));
ct.setSimpleColumn(signRect);
ct.go();
}
if (null != data.get("image")) {
int pageNo = form.getFieldPositions("image").get(0).page;
Rectangle signRect = form.getFieldPositions("image").get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
Image image = Image.getInstance(data.get("image").toString());
PdfContentByte under = pdfStamper.getOverContent(pageNo);
image.scaleAbsolute(signRect.getWidth(), signRect.getHeight());
image.setAbsolutePosition(x, y);
under.addImage(image);
}
pdfStamper.setFormFlattening(true);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
if (pdfStamper != null) {
pdfStamper.close();
}
if (pdfReader != null) {
pdfReader.close();
}
}
字体下载路径
https://yapp-image.oss-cn-shanghai.aliyuncs.com/customer/templateContract/f545c5c348ee419e81e7919c799fce6b.ttf