/**
* 创建初始化图片
*
* @param filePath 来源文件
* @param count 创建数量
*/
public static void createTemplatePictures(String filePath, int count) {
try (XWPFDocument document = new XWPFDocument(Files.newInputStream(new File(filePath).toPath()))) {
for (int i = 0; i < count; i++) {
document.createParagraph().createRun().addPicture(
new PathMatchingResourcePatternResolver().getResource("xxx.jpg").getInputStream(),
XWPFDocument.PICTURE_TYPE_JPEG,
"temp_picture_" + i + ".jpg",
1000000,
1000000);
}
File saveFile = new File("xxx.docx");
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
if (!saveFile.exists()) {
saveFile.createNewFile();
}
try (OutputStream outputStream = Files.newOutputStream(saveFile.toPath())) {
document.write(outputStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
} catch (Exception e) {
throw new RuntimeException("操作失败...");
}
}
POI在word中新增图片
于 2023-11-29 15:50:28 首次发布