首先引入的依赖
<!-- 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>
接下面的是template.docx文档,参数是以{{paramName}}格式的,但是从数据库获取的参数跟模板中的参数一一对应上即可,格式如下(我是json形式展示的):
{
"countQuota": "",
"noEmission": "",
"greenConsume": "",
"pollutCharge": "",
"emissionPermit": "",
"capitalOutlay": "",
"carbonTarget": "",
"zeroEmissionPower": "",
"kgce": "",
"productStandard": "",
"totalConsume": "",
"carbonEmission": "",
"consumePer": "",
"fileNames": "",
"directEmission": "",
"indirectEmission": "",
"partiEmission": ""
}
template.docx文档
大体上长这样
这里主要给图片中4.15排污许可证那里插入文件
具体代码如下:
package com.example.threaddemo.test;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.util.IOUtils;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
public class WordTest2 {
public static void main(String[] args) {
try {
// 加载Word文档
XWPFDocument document = new XWPFDocument(new FileInputStream("C:\\Users\\Administrator\\Desktop\\template.docx"));
// 获取文档中的表格列表
List<XWPFTable> tables = document.getTables();
// 遍历表格
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.equals("{{emissionPermit}}")) {
XWPFRun run = cell.getParagraphs().get(0).getRuns().get(0);
// 在字段位置插入图片
String imagePath = "C:\\Users\\Administrator\\Desktop\\1.png";
InputStream pngInputStream = new FileInputStream(imagePath);
byte[] pngBytes = IOUtils.toByteArray(pngInputStream);
//XWPFRun pngRun = targetParagraph.createRun();
run.setText("", 0);
run.addPicture(new ByteArrayInputStream(pngBytes), XWPFDocument.PICTURE_TYPE_PNG, "image.png", Units.toEMU(300), Units.toEMU(200));
pngInputStream.close();
// 在指定位置插入JPG图片
String jpgFilePath = "C:\\Users\\Administrator\\Desktop\\2.jpg";
InputStream jpgInputStream = new FileInputStream(jpgFilePath);
byte[] jpgBytes = IOUtils.toByteArray(jpgInputStream);
run.addPicture(new ByteArrayInputStream(jpgBytes), XWPFDocument.PICTURE_TYPE_JPEG, "image.jpg", Units.toEMU(300), Units.toEMU(200));
jpgInputStream.close();
// 在字段位置插入附件
String pdfPath = "C:\\Users\\Administrator\\Desktop\\滴滴电子发票.pdf";
String imagePath1 = "C:\\Users\\Administrator\\Desktop\\滴滴电子发票.png";
convertPdfToImage(pdfPath,imagePath1);
InputStream pngInputStream1 = new FileInputStream(imagePath1);
byte[] pngBytes1 = IOUtils.toByteArray(pngInputStream1);
//XWPFRun pngRun = targetParagraph.createRun();
run.setText("", 0);
run.addPicture(new ByteArrayInputStream(pngBytes1), XWPFDocument.PICTURE_TYPE_PNG, "image.png", Units.toEMU(300), Units.toEMU(200));
pngInputStream1.close();
}
//System.out.println(cellText);
}
}
}
// 保存修改后的文档
FileOutputStream outputStream = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.docx");
document.write(outputStream);
outputStream.close();
System.out.println("附件插入成功!");
} catch (Exception e) {
e.printStackTrace();
}
}
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();
}
}
以上代码只是插入了图片,其他的参数都没有变,如果需要将其它参数也改了,就用下面的代码
XWPFTemplate template = XWPFTemplate.compile(templatePath + File.separator + "template.docx").render(jsonObject);
template.write(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\1.docx"));
template.close();
在网上找了半天也么有什么好的方式可以在指定的位置直接将pdf插入进去,如果哪位大神有好的方式,可以留个言