java 结合 Adobe Acrobat DC工具 进行pdf打印
<!--PDF工具类 https://mvnrepository.com/artifact/com.srxlike.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>
<!-- PDF中文支持 https://mvnrepository.com/artifact/com.itextpdf/itext-asian -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
自己下载Adobe Acrobat
简单实用的截图
工具类
package com.zxy.elementstudent.util.poiEexcle;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.*;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;
/**
* @create: 2022-09-06 15:46
* @author: Dr.zxy
* @description:
**/
public class PDFUtil {
/**
* 利用模板生成pdf导出
*/
public static Integer pdfExportMethod(String templateFilePath, Map<String, String> dataMap, String newFilePath) {
// 模板路径
String templatePath = templateFilePath;
// 生成导出PDF的文件名称
String fileName = newFilePath;
OutputStream out = null;
ByteArrayOutputStream bos = null;
PdfStamper stamper = null;
PdfReader reader = null;
try {
// 读取PDF模板表单
reader = new PdfReader(templatePath);
// 字节数组流,用来缓存文件流
bos = new ByteArrayOutputStream();
// 根据模板表单生成一个新的PDF
stamper = new PdfStamper(reader, bos);
// 获取新生成的PDF表单
AcroFields form = stamper.getAcroFields();
// 给表单生成中文字体,这里采用系统字体,不设置的话,中文显示会有问题
BaseFont font = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
form.addSubstitutionFont(font);
// 遍历data,给pdf表单赋值
for (String key : dataMap.keySet()) {
// 图片要单独处理 规定 key值 与准备表单的属性 以 img开始
if (key.startsWith("img")) {
form.addSubstitutionFont(BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
//通过域名获取所在页和坐标,左下角为起点
int pageNo = form.getFieldPositions(key).get(0).page;
Rectangle signRect = form.getFieldPositions(key).get(0).position;
float x = signRect.getLeft();
float y = signRect.getBottom();
String studentImage = dataMap.get(key);
//根据路径或Url读取图片
Image image = Image.getInstance(studentImage);
//获取图片页面
PdfContentByte under = stamper.getOverContent(pageNo);
//图片大小自适应
image.scaleToFit(signRect.getWidth(), signRect.getHeight());
//添加图片
image.setAbsolutePosition(x, y);
under.addImage(image);
} else {
// 设置普通文本数据
form.setField(key, dataMap.get(key));
}
}
// 表明该PDF不可修改
stamper.setFormFlattening(true);
// 关闭资源
stamper.close();
// 将ByteArray字节数组中的流输出到out中(即输出到浏览器)
Document doc = new Document();
// 保存到本地
out = new FileOutputStream(fileName);
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
copy.addPage(importPage);
doc.close();
System.out.println("*****************************PDF导出成功*********************************");
return 1;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.flush();
out.close();
}
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return 0;
}
}
测试
@SneakyThrows
@Override
public Integer PDFStudentInfo(String ids, String fileTamplate, HttpServletResponse response)
throws UnsupportedEncodingException {
// 查询到的数据
StudentInfo studentInfo = studentInfoDao.getStudentInfoById(ids);
//模板位置
//String paths = "E:\\NewDream\\workCode\\vueshcoolfile\\student.pdf";
File file = ResourceUtils.getFile("classpath:template/" + fileTamplate);
String paths = file.getPath();
// 下载存储位置
String newPath = "E:\\NewDream\\workCode\\vueshcoolfile\\" + studentInfo.getName() + ".pdf";
//图片位置
String imgPath = "E:\\NewDream\\workCode\\vueshcoolfile\\gongzhang.png";
String imgPath1 = "E:\\NewDream\\workCode\\vueshcoolfile\\bj.png";
//键与Adobe Acrobat 的准备表单名称一样
Map<String, String> map = new HashMap<>();
map.put("name", studentInfo.getName());
map.put("img",imgPath);
map.put("img1",imgPath1);
map.put("sex", EnumSex.getByValue(studentInfo.getSex()).getName());
map.put("telphone", KeyAESUtil.decrypCode(studentInfo.getTelPhone()));
map.put("idcard", KeyAESUtil.decrypCode(studentInfo.getIdCard()));
map.put("adress", studentInfo.getAmId());
map.put("stuId", studentInfo.getStuId());
map.put("majar", EnumMajor.getByValue(studentInfo.getMajor()).getName());
map.put("classname", studentInfo.getClassName());
map.put("parantname", studentInfo.getPranteName());
map.put("parantphone", KeyAESUtil.decrypCode(studentInfo.getPranteIpone()));
map.put("classmastername", studentInfo.getClassMaster());
map.put("classmasternamepho", studentInfo.getClassMasterIpone());
map.put("nations", "汉族");
map.put("zhengzhiface", "团员");
map.put("college", EnumMajor.getByValue(studentInfo.getMajor()).getName());
map.put("mark", "");
String times = DateUtils.parseDayTime();
map.put("pdftiem", times);
map.put("year", "2022");
//调用方法
Integer result = PDFUtil.pdfExportMethod(paths, map, newPath);
return result == 1 ? 1 : 0;
}
本文含有隐藏内容,请 开通VIP 后查看