java word转pdf 扩展篇
前言
想必大家都使用了poi-tl进行模板填充,后有格式问题想转成pdf的问题吧
今天这篇文章就讲解一下word转pdf实现的操作,喜欢就点个赞收藏
一下!!!
一、数据准备
数据采用上篇文章的内容word:java poi-tl word模板填充
关键代码如下(示例):
public static void main(String[] args) throws Exception{
resolveTemp();
}
public static File resolveTemp() throws Exception {
String path = System.getProperty("user.dir") + "/file";
Map<String, Object> datas = new HashMap<>();
datas.put("dept", "采购部");
datas.put("name", "张三");
LocalDate date = LocalDate.now();
datas.put("year", date.getYear());
datas.put("month", date.getMonthValue());
datas.put("day", date.getDayOfMonth());
List<Map<String, Object>> itemList = new ArrayList<>();
Map<String, Object> itemMap = new HashMap<>();
itemMap.put("goods", "笔记本电脑");
itemMap.put("num", "2");
itemMap.put("cost", "10000");
itemMap.put("remark", "按需采购");
itemList.add(itemMap);
itemMap = new HashMap<>();
itemMap.put("goods", "台式电脑");
itemMap.put("num", "1");
itemMap.put("cost", "8000");
itemMap.put("remark", "按需采购");
itemList.add(itemMap);
datas.put("item", itemList);
datas.put("total", "18000.00");
//读取图片
File file = new File(path + "/1.png");
BufferedImage bi = ImageIO.read(file);
int width = bi.getWidth();
//源图高度
int height = bi.getHeight();
datas.put("image", new PictureRenderData(width, height, PictureType.PNG, new FileInputStream(file)));
System.out.println("datas = " + datas);
HackLoopTableRenderPolicy hackLoopTableRenderPolicy = new HackLoopTableRenderPolicy();
Configure config = Configure.builder()
.buildGramer("${", "}")//此处可以变更原模板${}
.bind("item", hackLoopTableRenderPolicy)//标记表格参数
.build();
String sourceFile = path + "/template/采购单.docx";
String docNow = LocalDateTimeUtils.formatDate(date, "yyyy年MM月dd日");
String targetFile = path + "/order/采购单" + docNow + ".docx";
XWPFTemplate template = XWPFTemplate.compile(new FileInputStream(sourceFile), config).render(datas);
File wordFile = new File(targetFile);
File parentFile = wordFile.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(wordFile);
template.write(out);
out.flush();
out.close();
template.close();
return wordFile;
}
word填充后效果
二、引入maven库
注意:以下使用的是aspose-words 15.8转换(poi-tl 版本 1.9.0-beta 对应 apache poi 版本 4.1.2)
jar包下载地址:aspose-word 15.8
代码如下(示例):
<!--word模板数据解析-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.9.0-beta</version>
</dependency>
<!-- aspose word转pdf -->
<dependency>
<groupId>aspose</groupId>
<artifactId>aspose-words</artifactId>
<version>15.8</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
</dependency>
AsposeUtil工具
package com.sam.utils;
import com.aspose.words.Document;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;
import lombok.SneakyThrows;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
/**
* @Author: Sam
* @Date: 2024-01-04 17:05
* @Description: Aspose工具类
*/
public class AsposeUtil {
/**
* 加载license 用于破解 不生成水印
*/
@SneakyThrows
private static void getLicense() {
try (InputStream is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml")) {
License license = new License();
license.setLicense(is);
}
}
/**
* word转pdf
*
* @param wordPath word文件保存的路径
* @param pdfPath 转换后pdf文件保存的路径
*/
@SneakyThrows
public static void wordToPdf(String wordPath, String pdfPath) {
getLicense();
File file = new File(pdfPath);
try (FileOutputStream os = new FileOutputStream(file)) {
Document doc = new Document(wordPath);
doc.save(os, SaveFormat.PDF);
}
}
}
license.xml
免责声明:至于license.xml商用的建议支持一下正版,因为不是免费
使用的
博客不公布代码license.xml
三、word转pdf
代码如下(示例):
public static void main(String[] args) throws Exception{
//resolveTemp()引用的上面的数据
File wordFile = resolveTemp();
String path = System.getProperty("user.dir") + "/file";
String pdfNow = LocalDateTimeUtil.format(LocalDateTime.now(), "yyyy年MM月dd日");
//创建空的pdf
File pdfFile = new File(path + "/order/" + "采购单" + pdfNow + ".pdf");
//pdf内容转换写入
AsposeUtil.wordToPdf(wordFile.getAbsolutePath(), pdfFile.getAbsolutePath());
}
pdf效果:
总结
以上就是今天要讲的内容,本文仅仅简单介绍了aspose-words的使用,对你有帮助就点个赞收藏下吧!
富哥哥可以支持打赏
一下
项目以上传gitee,对你有帮助可以star一下,项目地址:https://gitee.com/Sam997/poi-demo