在Aspose Word中的最后一页最底部添加一张图片,代码搞完后文档里面没有图片插入求解怎么解决
public void downloadApplyForm(String applyCode, String letterNoteId) {
CoAppLetterNoteData coAppLetterNoteData = this.repository.findOne(letterNoteId);
if (coAppLetterNoteData == null) {
throw new RuntimeException("没有找到文件");
}
//获取导出的文件数据
Map<String, Object> exportData = getExportData(coAppLetterNoteData);
//获取doc析对象
// String filePath = "/META-INF/template/" + applyCode + ".doc";
String fileName = null;
String filePath = null;
String imagePath = "/META-INF/template/" + "1.png";
if ("HzhzFileLetter".equals(coAppLetterNoteData.getLetterNoteType())) {
fileName = "油气合资合作事业部函件.doc";
filePath = "/META-INF/template/" + "letter.doc";
//判断函件类型
String meetStandard1 = CODE_BOX; //紧急
String meetStandard2 = CODE_BOX;//函复
String meetSite1 = CODE_BOX;//执行
String meetSite2 = CODE_BOX;//传阅
if ("HzhzUrgent".equals(coAppLetterNoteData.getFileType())) {
meetStandard1 = CODE_TICK;
} else if ("HzhzRfi".equals(coAppLetterNoteData.getFileType())) {
meetStandard2 = CODE_TICK;
} else if ("HzhzPerform".equals(coAppLetterNoteData.getFileType())) {
meetSite1 = CODE_TICK;
} else if ("HzhzFyi".equals(coAppLetterNoteData.getFileType())) {
meetSite2 = CODE_TICK;
}
exportData.put("meetsta1", meetStandard1);
exportData.put("meetsta2", meetStandard2);
exportData.put("meetsit1", meetSite1);
exportData.put("meetsit2", meetSite2);
} else {
fileName = "油气合资合作事业部纪要.doc";
filePath = "/META-INF/template/" + "summary.doc";
}
try {
//将图片转为字符流
imagePath=HzhzofficeAutoConfiguration.class.getResource("/META-INF/template/1.png").getPath();
imagePath=URLDecoder.decode(imagePath, "UTF-8");
imagePath=imagePath.substring(1);
// 获取图片的字节数组
File file = new File(imagePath);
BufferedImage image = ImageIO.read(file);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
byte[] bytes = baos.toByteArray();
Map<String, byte[]> imageMap = new HashMap<>();
imageMap.put("image", bytes);
// 读取模板文件
InputStream fileInputStream = this.getClass().getResourceAsStream(filePath);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n;
while (-1 != (n = fileInputStream.read(buffer))) {
output.write(buffer, 0, n);
}
byte[] modelByte = output.toByteArray();
//将图片相对路径转化为绝对路径
imagePath = HzhzofficeAutoConfiguration.class.getResource("/META-INF/template/1.png").getPath();
imagePath = URLDecoder.decode(imagePath, "UTF-8");
imagePath = imagePath.substring(1);
//将文件的相对路径转换为绝对路径
filePath = HzhzofficeAutoConfiguration.class.getResource(filePath).getPath();
//设置路径编码
filePath = URLDecoder.decode(filePath, "utf-8");
//把路径前的“/”截取
filePath = filePath.substring(1);
// 调用工具类,获取填充数据后的文件
byte[] resultByte = fillWordDataByMap(modelByte, exportData, imagePath, filePath);
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletResponse response = Objects.requireNonNull(requestAttributes).getResponse();
Objects.requireNonNull(response).setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(fileName + ".doc", "UTF-8"));
OutputStream out = response.getOutputStream();
out.write(resultByte);
out.close();
response.getOutputStream().flush();
} catch (Exception ex) {
ex.printStackTrace();
}
}
/**
* 填充 word 模板(map数据格式)
*
* @param file word二进制
* @param data 要填充的数据
* @return 组合数据之后的word二进制
*/
private static byte[] fillWordDataByMap(byte[] file, Map<String, Object> data, String imagePath, String filePath) throws Exception {
byte[] ret = null;
if (data == null || data.isEmpty()) {
return ret;
}
try (InputStream is = new ByteArrayInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
Document doc = new Document(is);
DocumentBuilder builder = new DocumentBuilder(doc);
Map<String, String> toData = new HashMap<>(16);
for (Map.Entry<String, Object> en : data.entrySet()) {
String key = en.getKey();
Object value = en.getValue();
if (value instanceof List) {
//写入表数据
DataTable dataTable = fillListData((List) value, key, builder);
doc.getMailMerge().executeWithRegions(dataTable);
}
if (value instanceof BufferedImage) {
builder.moveToMergeField(key);
BufferedImage image = (BufferedImage) value;
ImageIO.write(image, "png", new File(imagePath));
builder.moveToBookmark("image");
builder.insertImage(imagePath);
}
if (value instanceof byte[]) {
builder.moveToMergeField(key);
byte[] imageBytes = (byte[]) value;
FileOutputStream fos = new FileOutputStream(filePath);
fos.write(imageBytes);
fos.close();
builder.moveToBookmark("image");
builder.insertImage(filePath);
}
//方框打勾
if (CODE_TICK.equals(value)) {
builder.moveToMergeField(key);
//设置字体
builder.getFont().setName("Wingdings 2");
builder.write("\uF052");
}
//方框
if (CODE_BOX.equals(value)) {
builder.moveToMergeField(key);
//设置字体
builder.getFont().setName("Wingdings 2");
builder.write("\uF0A3");
}
//为空则默认空字符串
String valueStr = String.valueOf(en.getValue());
if (value == null || "null".equals(value)) {
valueStr = " ";
}
toData.put(key, valueStr);
}
String[] fieldNames = new String[toData.size()];
String[] values = new String[toData.size()];
int i = 0;
for (Map.Entry<String, String> entry : toData.entrySet()) {
fieldNames[i] = entry.getKey();
values[i] = entry.getValue();
i++;
}
//合并数据
doc.getMailMerge().execute(fieldNames, values);
doc.save(out, SaveOptions.createSaveOptions(SaveFormat.DOC));
ret = out.toByteArray();
}
return ret;
}
这是模板截图: