代码实现 <-- 导入pdf相关依赖 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.4.3</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.12</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.12</version>
</dependency>
public static void word2Pdf(Map<String, Object> o) throws Exception {
// 模板路径
String templatePath = "D://muban.pdf";
// 生成的新文件路径
String newPDFPath = "D://testout1.pdf";
PdfReader reader;
FileOutputStream out;
ByteArrayOutputStream bos;
PdfStamper stamper;
BaseFont bf = BaseFont.createFont("C://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
out = new FileOutputStream(newPDFPath); // 输出流
reader = new PdfReader(templatePath); // 读取pdf模板
bos = new ByteArrayOutputStream();
stamper = new PdfStamper(reader, bos);
AcroFields form = stamper.getAcroFields();
// 文字类的内容处理
Map<String, String> dataMap = (Map<String, String>) o.get("dataMap");
form.addSubstitutionFont(bf);
for (String key : dataMap.keySet()) {
String value = dataMap.get(key);
form.setField(key, value);
}
// 图片类的内容处理
Map<String, String> imgMap = (Map<String, String>) o.get("imgMap");
if (imgMap != null && !imgMap.isEmpty()) {
for (String key : imgMap.keySet()) {
String value = imgMap.get(key);
String imgPath = value; // 图片本地地址
// String imgURLPath = value; // 图片网络url
URL url = new URL(imgURLPath);
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);
// Image image = Image.getInstance(url);
// 获取图片页面
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();
PdfCopy copy = new PdfCopy(doc, out);
doc.open();
// 多页处理
int numberOfPages = reader.getNumberOfPages();
for (int i = 0; i < numberOfPages; i++) {
PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), i + 1);
copy.addPage(importPage);
}
doc.close();
}
public static void main(String[] args) throws Exception {
Map<String, String> text = new HashMap();
Map<String, String> image = new HashMap();
String jbqk = " 项目位于*********(东经:111.404074°,北纬:30.383564°),项目地块面积1.05亩。土地利用现状:村庄0.91亩、果园0.13亩;土地权属为集体1.05亩。";
text.put("jbqk", jbqk); // 基本情况
image.put("xmqywz", "D:a.jpg"); // 项目区域位置
image.put("tdlyxzt", "D:b.jpg"); // 土地利用现状图
String yjjbntqkfx = " 拟建设项目区不涉及永久基本农田。";
text.put("yjjbntqkfx", yjjbntqkfx); // 永久基本农田情况分析
String zdqpdjfx = " 拟建设项目区征地区片地价未知";
text.put("zdqpdjfx", zdqpdjfx); // 征地区片地价分析
String dzzhyhfx = " 拟建设项目区地质灾害隐患情况不详(无该区域地质灾害数据)。";
text.put("dzzhyhfx", dzzhyhfx); // 地质灾害隐患分析
String stydfx = "拟建设项目区不占用湿地。";
text.put("stydfx", stydfx); // 生态用地分析
String ztfx = " 根据已有数据分析结果,拟建设项目区不合法、不适宜。";
text.put("ztfx", ztfx); // 总体分析
String bgr = "报告人:张三";
text.put("bgr", bgr); // 报告人
String sj = "时间:" + new SimpleDateFormat("YYYY-MM-dd").format(new Date());
text.put("sj", sj); // 时间
Map<String, Object> o = new HashMap();
o.put("dataMap", text);
o.put("imgMap", image);
try {
word2Pdf(o);
} catch (Exception e) {
e.printStackTrace();
}
}