当前位置: 代码网 > it编程>编程语言>Java > java根据模板实现填充word内容并转换为pdf

java根据模板实现填充word内容并转换为pdf

2025年04月08日 Java 我要评论
1.word模板填充内容使用easypoi写入word文档。import cn.afterturn.easypoi.word.wordexportutil;import org.apache.comm

1.word模板填充内容

使用easypoi写入word文档。

import cn.afterturn.easypoi.word.wordexportutil;
import org.apache.commons.io.fileutils;
import org.apache.commons.io.ioutils;
import org.apache.poi.xwpf.usermodel.xwpfdocument;

import java.io.file;
import java.io.fileoutputstream;
import java.util.arraylist;
import java.util.hashmap;
import java.util.list;
import java.util.map;


public class writewordtemplate {


    public static void main(string[] args) throws exception {
        map<string, object> params = createdate();
        string sourcefile = "d:/temp/模版word3.docx";
        string targetfile = "d:/temp/输出结果3.docx";
        fileoutputstream fos = null;
        try {
            //获取模板文档
            file templatefile = new file(sourcefile);
            system.out.println(templatefile.getname());
            // 写入word
            xwpfdocument doc = wordexportutil.exportword07(templatefile.getpath(), params);
            fos = fileutils.openoutputstream(new file(targetfile));
            doc.write(fos);
        } catch (exception e) {
            system.out.println(e);
        } finally {
            ioutils.closequietly(fos);
        }
    }

    private static map<string, object> createdate() {

        //填充数据
        list<wordexportbatch> resultlist = new arraylist<>();
        wordexportbatch wordexport = new wordexportbatch();
        wordexportbatch wordexport1 = new wordexportbatch();
        wordexport.setcreatedate("2022/9/30");
        wordexport1.setcreatedate("2022/9/28");
        wordexport.setnumber("11");
        wordexport.setexmoneny("11");
        wordexport.setexfunc("支付宝");
        wordexport1.setnumber("15");
        wordexport.setamount("1234.5");
        wordexport1.setamount("2345.77");
        wordexport.setenddate("2022/12/31");
        wordexport1.setenddate("2022/11/30");
        wordexport.settype("支付宝");
        wordexport1.settype("微信");
        wordexport1.setexmoneny("22");
        wordexport1.setexfunc("微信");
        resultlist.add(wordexport);
        resultlist.add(wordexport1);
        //准备数据
        map<string, object> params = new hashmap<>();
        params.put("number", "112");
        params.put("amount", "1234.5");
        params.put("enddate", "2022/11/30");
        params.put("resultlist", resultlist);
        return params;
    }

}

pom依赖可以参考:使用easypoi实现word文档生成和段落循环

2.把word转换为pdf

word生成pdf的方法比较多,调研了常用的方式

一种方式是部署一台windowserver,对外提供接口来进行生成,能最大程度还原格式。

第二种方式就是利用jodconverter,会有少许的失真。

现在介绍的是基于jodconverter把word转换为pdf。首先引入jar包

        <dependency>
            <groupid>org.jodconverter</groupid>
            <artifactid>jodconverter-local</artifactid>
            <version>4.2.2</version>
        </dependency>
        <dependency>
            <groupid>org.jodconverter</groupid>
            <artifactid>jodconverter-spring-boot-starter</artifactid>
            <version>4.2.2</version>
        </dependency>

在配置文件中加入如下配置

jodconverter:
  local:
    enabled: true             # 启用本地模式
    office-home: /opt/libreoffice7.5    # libreoffice 的安装路径
    max-tasks-per-process: 10 # 每个 office 进程处理的最大任务数
    port-numbers: 8202        # libreoffice 后台服务使用的端口号,可修改

在处理类中注入,之所以使用@lazy是因为加载的使用会报错,让它延迟加载。

     @lazy
   @autowired
   private documentconverter converter;
    
 public void wordconverttopdf(file wordfile, file pdffile) {
        try {
            logger.info("word转换pdf开始");
            converter.convert(wordfile).to(pdffile).execute();
            logger.info("word转换pdf结束");
        } catch (exception e) {
            logger.error("word转pdf异常", e);
        }
    }

3.后续使用

之前写过word模板生成段落和生成表格的段落。

结合起来就能生成相对完整的word了。后续如果需要盖章什么的直接拿生成的pdf文件就可以了。

到此这篇关于java根据模板实现填充word内容并转换为pdf的文章就介绍到这了,更多相关java模板填充word内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com