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>