Java填充word配置模版并将word转pdf

首先准备word模版配置信息,如下图所示

pom.xml需要引入以下jar包 

<dependency>
    <groupId>com.deepoove</groupId>
    <artifactId>poi-tl</artifactId>
    <version>1.5.0</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.83</version>
</dependency>
<dependency>
    <groupId>com.luhuiguo</groupId>
    <artifactId>aspose-words</artifactId>
    <version>23.1</version>
</dependency>
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.7</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml-schemas</artifactId>
    <version>3.15</version>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.83</version>
</dependency>
<dependency>
    <groupId>com.luhuiguo</groupId>
    <artifactId>aspose-words</artifactId>
    <version>23.1</version>
</dependency>

根据word模版内容填充数据代码内容如下

Map params  = new HashMap<>();
params.put("today", TimeTransferUtil.todayTimeString());
params.put("year", TimeTransferUtil.yearTimeString());
params.put("number", String.format("%03d", code));
WordUtil.createWord("word模版文档路径", "内容填充后word生成路径","填充参数");

WordUtil代码内容

public class WordUtil {
    public static boolean createWord(String templatePath, String filePath, Map<String, Object> paramMap) {
        // 读取模板templatePath并将paramMap的内容填充进模板,即编辑模板+渲染数据
        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramMap);
        try {
            // 将填充之后的模板写入filePath
            template.writeToFile(filePath);
            template.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}

Word转pdf的代码

inPath:word文件路径

outPath:pdf文件输出路径

public static void doc2pdf(String inPath, String outPath) {
    FileOutputStream os = null;
    try {
        File file = new File(outPath);
        os = new FileOutputStream(file);
        Document doc = new Document(inPath);
        doc.save(os, SaveFormat.PDF);


    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我已经理解您的问题。您可以使用Apache POI库和iText库来实现这个需求。下面是一个简单的Java代码示例: 1. 首先,您需要使用Apache POI库来创建一个Word模板文件。 ```java // 创建一个Word模板文件 XWPFDocument doc = new XWPFDocument(); XWPFParagraph p1 = doc.createParagraph(); XWPFRun r1 = p1.createRun(); r1.setText("Hello, World!"); // 将模板文件保存到本地 FileOutputStream out = new FileOutputStream("template.docx"); doc.write(out); out.close(); ``` 2. 然后,您可以使用Apache POI库来填充模板文件中的占位符。 ```java // 打开Word模板文件,并替换其中的占位符 XWPFDocument doc = new XWPFDocument(new FileInputStream("template.docx")); XWPFParagraph p1 = doc.getParagraphs().get(0); XWPFRun r1 = p1.getRuns().get(0); String text = r1.getText(0); text = text.replace("World", "Java"); r1.setText(text, 0); // 将填充后的模板文件保存到本地 FileOutputStream out = new FileOutputStream("output.docx"); doc.write(out); out.close(); ``` 3. 最后,您可以使用iText库将填充后的Word文件换为PDF文件。 ```java // 打开填充后的Word文件,并将其换为PDF文件 XWPFDocument doc = new XWPFDocument(new FileInputStream("output.docx")); PdfWriter writer = new PdfWriter("output.pdf"); PdfDocument pdf = new PdfDocument(writer); Converter.convert(doc, pdf); pdf.close(); writer.close(); ``` 注意:以上代码示例仅供参考,具体的实现方式可能会因您的具体需求而有所不同。您需要根据您的需求进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值