POI 实现 word转成pdf
想系统学习的同志,可以参考 POI官方
1、导出依赖
<dependency> <groupId>fr.opensagres.xdocreport</groupId> <artifactId>fr.opensagres.poi.xwpf.converter.pdf-gae</artifactId> <version>2.0.1</version> </dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.2.0</version>
</dependency>
这里要注意版本问题!!
2、准备word稳当
3、实现代码1
/** * 将word模板转化成pdf * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Map datas = new HashMap(); datas.put("title","我是活动标题"); //给{{title}}标识赋值 XWPFTemplate template = XWPFTemplate.compile("E:/test.docx"); template.render(datas);
//将word转成pdf PdfOptions options = PdfOptions.create(); try (OutputStream outPDF = Files.newOutputStream(Paths.get("E:/tes2.pdf"))) { PdfConverter.getInstance().convert(template.getXWPFDocument(), outPDF, options); } catch (IOException e) { e.printStackTrace(); } }
4、实现代码2,这里不考虑赋值问题,直接转换
public static void main(String[] args) { try {
//读取word文档 XWPFDocument document = null; try (InputStream in = Files.newInputStream(Paths.get("E:/test1.docx"))) { document = new XWPFDocument(in); } catch (IOException e) { e.printStackTrace(); }
//将word转成pdf PdfOptions options = PdfOptions.create(); try (OutputStream outPDF = Files.newOutputStream(Paths.get("E:/tes2.pdf"))) { PdfConverter.getInstance().convert(document, outPDF, options); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
5、在E盘中查看效果
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· 葡萄城 AI 搜索升级:DeepSeek 加持,客户体验更智能
· 什么是nginx的强缓存和协商缓存
· 一文读懂知识蒸馏