|
@@ -2,12 +2,16 @@ package com.onemap.analyse.utils;
|
|
|
|
|
|
import com.aspose.words.*;
|
|
|
import com.onemap.common.core.utils.StringUtils;
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.util.Units;
|
|
|
import org.apache.poi.xwpf.usermodel.*;
|
|
|
import org.apache.poi.xwpf.usermodel.LineSpacingRule;
|
|
|
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
|
|
|
import org.aspectj.weaver.ast.Test;
|
|
|
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
|
|
|
|
|
|
+import javax.imageio.ImageIO;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.math.BigInteger;
|
|
@@ -520,4 +524,42 @@ public class NpoiHelper {
|
|
|
}
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public static void addImagesToWord(XWPFDocument document, File[] imgs) throws IOException, InvalidFormatException {
|
|
|
+// try (XWPFDocument document = new XWPFDocument()) {
|
|
|
+ for (File img : imgs) {
|
|
|
+ // 只对指定后缀的图片处理
|
|
|
+ if (img.getName().endsWith("jpeg") || img.getName().endsWith("jpg") || img.getName().endsWith("png")) {
|
|
|
+ BufferedImage bufferedImage = ImageIO.read(img);
|
|
|
+ int imageType = XWPFDocument.PICTURE_TYPE_JPEG;
|
|
|
+ if (img.getName().endsWith(".png")) {
|
|
|
+ imageType = XWPFDocument.PICTURE_TYPE_PNG;
|
|
|
+ }
|
|
|
+
|
|
|
+ int width = bufferedImage.getWidth();
|
|
|
+ int height = bufferedImage.getHeight();
|
|
|
+
|
|
|
+ // Create a new run and add the image
|
|
|
+ XWPFRun run = document.createParagraph().createRun();
|
|
|
+// log.warn("fileName:{},height:{},width:{}", img.getName(), height, width);
|
|
|
+ run.addPicture(new FileInputStream(img), imageType, img.getName(), Units.toEMU(width), Units.toEMU(height));
|
|
|
+ // Optionally, you can add more text or formatting here
|
|
|
+
|
|
|
+ // Add a new line for the next image
|
|
|
+// document.createParagraph();
|
|
|
+ } else {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+// // Save the modified document
|
|
|
+// try (FileOutputStream fos = new FileOutputStream("D:\\tmp\\tmp\\20231208\\modify.doc")) {
|
|
|
+// document.write(fos);
|
|
|
+// System.out.println("Images added to Word document successfully.");
|
|
|
+// }
|
|
|
+ }
|
|
|
+// } catch (InvalidFormatException e) {
|
|
|
+// throw new RuntimeException(e);
|
|
|
+// }
|
|
|
+ }
|
|
|
}
|