Java将获取的参数,图片以及pdf文件放入到word文档指定位置

首先引入的依赖

<!-- poi库 -->  
       <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
<!-- Apache PDFBox库(用于处理PDF文件) -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.27</version>
        </dependency>

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

接下面的是template.docx文档,参数是以{{paramName}}格式的,为什么要以这种格式,是因为下面的方法,在替换参数的时候需要

XWPFTemplate template = XWPFTemplate.compile( "C:\\Users\\Administrator\\Desktop\\template.docx").render(jsonObject);

但是从数据库获取的参数跟模板中的参数一一对应上即可,格式如下(我是json形式展示的):

{
    "countQuota": "1",
    "noEmission": "1",
    "greenConsume": "1",
    "pollutCharge": "1",
    "emissionPermit": "C:\\Users\\Administrator\\Desktop\\",
    "capitalOutlay": "1",
    "carbonTarget": "1",
    "zeroEmissionPower": "",
    "kgce": "",
    "productStandard": "",
    "totalConsume": "",
    "carbonEmission": "",
    "consumePer": "",
    "fileNames": "1.png,2.jpg,3.pdf",
    "directEmission": "",
    "indirectEmission": "1",
    "partiEmission": "1"
}

template.docx文档

大体上长这样

 这里主要给图片中4.15排污许可证那里插入文件

具体代码如下:

package com.example.threaddemo.test;


import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.deepoove.poi.XWPFTemplate;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Map;

public class WordTest3 {
    public static void main(String[] args) throws Exception {
        String str = "{\n" +
                "\t\"countQuota\": \"1\",\n" +
                "\t\"noEmission\": \"1\",\n" +
                "\t\"greenConsume\": \"1\",\n" +
                "\t\"pollutCharge\": \"1\",\n" +
                "\t\"emissionPermit\": \"C:\\\\Users\\\\Administrator\\\\Desktop\\\\\",\n" +
                "\t\"capitalOutlay\": \"1\",\n" +
                "\t\"carbonTarget\": \"1\",\n" +
                "\t\"zeroEmissionPower\": \"\",\n" +
                "\t\"kgce\": \"\",\n" +
                "\t\"productStandard\": \"\",\n" +
                "\t\"totalConsume\": \"\",\n" +
                "\t\"carbonEmission\": \"\",\n" +
                "\t\"consumePer\": \"\",\n" +
                "\t\"fileNames\": \"1.png,2.jpg,滴滴电子发票.pdf\",\n" +
                "\t\"directEmission\": \"\",\n" +
                "\t\"indirectEmission\": \"1\",\n" +
                "\t\"partiEmission\": \"1\"\n" +
                "}";
        //str = str.replace("\n","");
        //str.replace("}","\"}");
        //str = str.replaceAll("\\\\667A", "667A");

        System.out.println("str = " + str);

        //Map<String, String> jsonObject = JSONObject.parseObject(str, Map.class);
        JSONObject jsonObject = JSONObject.parseObject(str);

        //.render(jsonObject) json,map或者实体类都是可以的,只要参数能对上就可以了
        XWPFTemplate template = XWPFTemplate.compile( "C:\\Users\\Administrator\\Desktop\\generate-template.docx").render(jsonObject);
        template.write(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.docx"));
        template.close();

        if (StringUtils.isNotBlank(jsonObject.getString("emissionPermit"))) {//说明排污许可证上传了
            XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\Users\\Administrator\\Desktop\\1.docx"));
            // 获取文档中的表格列表
            List<XWPFTable> tables = document.getTables();
            String path_pre = jsonObject.getString("emissionPermit");
            String fileNames = jsonObject.getString("fileNames");
            // 遍历表格
            for (XWPFTable table : tables) {
                // 遍历表格的行
                List<XWPFTableRow> rows = table.getRows();
                for (XWPFTableRow row : rows) {
                    // 遍历行的单元格
                    List<XWPFTableCell> cells = row.getTableCells();
                    for (XWPFTableCell cell : cells) {
                        // 获取单元格的文本内容
                        String cellText = cell.getText();
                        if (cellText.contains(path_pre)) {
                            XWPFRun run = cell.getParagraphs().get(0).getRuns().get(0);
                            run.setText("", 0);//置空里面的参数{{emissionPermit}}

                            for (String filename : fileNames.split(",")) {
                                String file_path = path_pre + filename;
                                System.out.println("file_path = " + file_path);
                                // 加载Word文档
                                if (file_path.endsWith(".png")) {
                                    int type = XWPFDocument.PICTURE_TYPE_PNG;
                                    insertImage(file_path, type, run, filename);
                                }
                                if (file_path.endsWith(".jpg")) {
                                    int type = XWPFDocument.PICTURE_TYPE_JPEG;
                                    insertImage(file_path, type, run, filename);
                                }
                                if (file_path.endsWith(".pdf")) {
                                    String newFilePath = file_path.replace(".pdf", ".png");
                                    convertPdfToImage(file_path, newFilePath);
                                    int type = XWPFDocument.PICTURE_TYPE_PNG;
                                    insertImage(newFilePath, type, run, filename);
                                    File newFile = new File(newFilePath);
                                    newFile.delete();//把pdf生成的png图片删除
                                }

                            }
                            // 保存修改后的文档
                            FileOutputStream outputStream1 = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.docx");
                            document.write(outputStream1);
                            outputStream1.close();
                        }
                    }
                }


            }

        }
        //jsonObject.remove("emissionPermit");

    }


    //将pdf转为png
    private static void convertPdfToImage(String pdfPath, String imagePath) throws IOException {
        PDDocument document = PDDocument.load(new File(pdfPath));
        PDFRenderer renderer = new PDFRenderer(document);
        BufferedImage image = renderer.renderImage(0); // 渲染第一页为图像
        ImageIO.write(image, "PNG", new File(imagePath));
        document.close();
    }

    //将图片插入到指定位置
    private static void insertImage(String filePath, int type, XWPFRun run, String fileName) {
        try (InputStream pngInputStream = new FileInputStream(filePath)) {
            byte[] jpgBytes = IOUtils.toByteArray(pngInputStream);
            run.addPicture(new ByteArrayInputStream(jpgBytes), type, fileName, Units.toEMU(300), Units.toEMU(200));
        } catch (Exception e) {
            //LOGGER.info("tcfd插入图片异常,异常原因:",e.getMessage(),e);
            throw new RuntimeException(e);
        }
    }
}

在网上找了半天也么有什么好的方式可以在指定的位置直接将pdf插入进去,如果哪位大神有好的方式,可以留个言

如果在运行的过程中有这个报错:java.lang.NoClassDefFoundError: org/apache/fontbox/cmap/CMapParser

请加下下面的依赖

        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>fontbox</artifactId>
            <version>2.0.27</version>
        </dependency>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,你可以使用 Apache POI 和 iText 库来实现将 Word 文档转换成 PDF 文件的功能。首先,确保你已经将这两个库添加到你的项目中。 以下是一个简单的 Java 代码示例,可以将 Word 文档转换为 PDF 文件: ```java import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class WordToPdfConverter { public static void main(String[] args) { String inputWordFile = "input.docx"; // 输入的 Word 文件路径 String outputPdfFile = "output.pdf"; // 输出的 PDF 文件路径 try { // 打开 Word 文档 XWPFDocument document = new XWPFDocument(new FileInputStream(inputWordFile)); // 创建 PDF 文档 Document pdfDoc = new Document(); PdfWriter.getInstance(pdfDoc, new FileOutputStream(outputPdfFile)); // 打开 PDF 文档 pdfDoc.open(); // 读取 Word 文档中的段落内容,并将其写入 PDF 文档 for (XWPFParagraph paragraph : document.getParagraphs()) { String text = paragraph.getText(); pdfDoc.add(new Paragraph(text)); } // 关闭 PDF 文档 pdfDoc.close(); System.out.println("Word 文档成功转换为 PDF 文件!"); } catch (IOException e) { System.out.println("转换过程出现错误:" + e.getMessage()); } } } ``` 在代码中,你需要将 `input.docx` 替换为你要转换的 Word 文档路径,`output.pdf` 替换为输出的 PDF 文件路径。请确保输入的 Word 文档是正确的,且具有正确的扩展名(.docx)。 希望这段代码对你有所帮助!如果有任何问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值