POI 实现 word转成pdf
想系统学习的同志,可以参考 POI官方
1、导出依赖
fr.opensagres.xdocreport
fr.opensagres.poi.xwpf.converter.pdf-gae
2.0.1
com.deepoove poi-tl 1.2.0
这里要注意版本问题!!
2、准备word稳当
![](https://img2020.cnblogs.com/blog/1707816/202109/1707816-20210920212109702-2018984888.png)
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盘中查看效果
![](https://img2020.cnblogs.com/blog/1707816/202109/1707816-20210920212733493-280418691.png)
![](https://img2020.cnblogs.com/blog/1707816/202109/1707816-20210920212808805-804978013.png)
posted on
2021-09-20 21:28
挽留匆匆的美丽
阅读(10433)
评论(2)
编辑
收藏
举报
|