java word转pdf,word模板

maven

复制代码
<dependency>
	<groupId>org.apache.poi</groupId>
	<artifactId>poi-ooxml</artifactId>
	<version>5.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.xdocreport.converter.docx.xwpf -->
<dependency>
	<groupId>fr.opensagres.xdocreport</groupId>
	<artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
	<version>2.0.4</version>
</dependency>


<dependency>
	<groupId>com.deepoove</groupId>
	<artifactId>poi-tl</artifactId>
	<version>1.12.1</version>
</dependency>

poi-tl语法参考文档

http://deepoove.com/poi-tl/

测试poi-tl

复制代码
import com.deepoove.poi.XWPFTemplate;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestPoiTl {
    public static void main(String[] args) throws Exception {

        List<Map<String,Object>> users = new ArrayList<>();

        users.add(user("张三",10,"zs"));
        users.add(user("李四",20,"ls"));

        XWPFTemplate template = XWPFTemplate.compile("D:\\wordpdf\\template.docx").render(
                new HashMap<String, Object>(){{
                    put("users", users);
                }});
        template.writeAndClose(new FileOutputStream("D:\\wordpdf\\output.docx"));
    }

    static Map<String,Object> user(String name,int age,String account){
        Map<String,Object> user = new HashMap<>();
        user.put("name",name);
        user.put("age",age);
        user.put("account",account);

        return user;
    }

}

测试word转pdf

复制代码
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

public class word2pdf {

    /**
     * @param args the command line arguments
     * @throws java.io.IOException
     */
    public static void main(String[] args) throws IOException {
        String docPath = "D:\\wordpdf\\test.docx";
        String pdfPath = "D:\\wordpdf\\test.pdf";

        XWPFDocument document;
        InputStream doc = new FileInputStream(docPath);
        document = new XWPFDocument(doc);
        PdfOptions options = PdfOptions.create();
        OutputStream out = new FileOutputStream(pdfPath);
        PdfConverter.getInstance().convert(document, out, options);

        doc.close();
        out.close();
    }

}

test.docx 截图

template.docx截图

效果截图

相关推荐
2401_894828126 分钟前
从原理到实战:随机森林算法全解析(附 Python 完整代码)
开发语言·python·算法·随机森林
玄同7657 分钟前
Python「焚诀」:吞噬所有语法糖的终极修炼手册
开发语言·数据库·人工智能·python·postgresql·自然语言处理·nlp
羽翼.玫瑰9 分钟前
关于重装Python失败(本质是未彻底卸载Python)的问题解决方案综述
开发语言·python
cdut_suye9 分钟前
解锁函数的魔力:Python 中的多值传递、灵活参数与无名之美
java·数据库·c++·人工智能·python·机器学习·热榜
CRMEB系统商城38 分钟前
CRMEB多商户系统(PHP)- 移动端二开之基本容器组件使用
运维·开发语言·小程序·php
淮北49443 分钟前
科研绘图工具R语言
开发语言·r语言
逍遥德1 小时前
java Map Set List 扩容机制
java·开发语言·list
高山上有一只小老虎1 小时前
mybatisplus实现分页查询
java·spring boot·mybatis
2501_944521591 小时前
Flutter for OpenHarmony 微动漫App实战:图片加载实现
android·开发语言·前端·javascript·flutter·php
nbsaas-boot1 小时前
基于 Java 21 ScopedValue 的多租户动态数据源完整实践
java·开发语言