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截图

效果截图

相关推荐
anlogic40 分钟前
Java基础 8.18
java·开发语言
沐知全栈开发1 小时前
WebForms XML 文件详解
开发语言
练习时长一年1 小时前
AopAutoConfiguration源码阅读
java·spring boot·intellij-idea
阿巴~阿巴~2 小时前
冒泡排序算法
c语言·开发语言·算法·排序算法
源码宝3 小时前
【智慧工地源码】智慧工地云平台系统,涵盖安全、质量、环境、人员和设备五大管理模块,实现实时监控、智能预警和数据分析。
java·大数据·spring cloud·数据分析·源码·智慧工地·云平台
看到我,请让我去学习3 小时前
QT - QT开发进阶合集
开发语言·qt
weixin_307779133 小时前
VS Code配置MinGW64编译SQLite3库
开发语言·数据库·c++·vscode·算法
David爱编程3 小时前
面试必问!线程生命周期与状态转换详解
java·后端
LKAI.4 小时前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
HeyZoeHey4 小时前
Mybatis执行sql流程(一)
java·sql·mybatis