1.代码如下 package net.longjin.configCenter.caseConfig.controller; import com.aspose.words.*; import com.sun.javafx.collections.MappingChange; import net.longjin.comm.utils.AsposeWordsUtils; import net.longjin.comm.utils.PDFUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.InputStream; import java.util.*; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.aspose.words.Document; import com.aspose.words.License; import com.aspose.words.Node; import com.aspose.words.NodeType; import com.aspose.words.Range; import com.aspose.words.Table; /** * 描述:Test * * @author 何志鹏 * @ClassName:Test * @create 2019-07-08 14:06 * Version 1.0 */ public class Test { private static final Logger LOGGER = LoggerFactory.getLogger(net.longjin.configCenter.caseConfig.controller.AsposeUtils.class); public static boolean getLicense() { boolean result = false; try { InputStream is = net.longjin.configCenter.caseConfig.controller.AsposeUtils.class.getClassLoader().getResourceAsStream("com.aspose.words.lic.xml"); License aposeLic = new License(); aposeLic.setLicense(is); result = true; } catch (Exception e) { e.printStackTrace(); } return result; } /** * 保存doc path保存目录 doc原文档 * * @param * @param * @throws Exception */ public static void main(String[] agrs) throws Exception { if (!getLicense()) {// 验证License 若不验证则转化出的pdf文档会有水印产生 return; } long old = System.currentTimeMillis(); String path = "C:/Users/longjin/Desktop/证据照片(图片)登记表.docx"; Document doc = new Document(path); // 获取word中的第一个表格,index指定表格位置 Table table = (Table) doc.getChild(NodeType.TABLE, 0, true); DocumentBuilder builder = new DocumentBuilder(doc); String patch = "D:/aa.jpg"; String patch2 = "D:/bb.jpg"; String patch3 = "D:/cc.jpg"; List<String> list = new ArrayList<>(); list.add(patch); list.add(patch2); list.add(patch3); Range range = null; for (int i = 0;i<list.size();i++) { //复制行 Node deepClone = table.getRows().get(4).deepClone(true);//复制第五行 table.getRows().insert(4,deepClone);//将复制的行放在第五行后面 } table.getRows().get(4).remove(); for (int i = 0;i<list.size();i++) { // 循环插入指定表格的图片 builder.moveToCell(0,4+i,0,0); builder.insertImage(list.get(i), RelativeHorizontalPosition.MARGIN, 200, RelativeVerticalPosition.MARGIN, 1, 100, 125, WrapType.SQUARE); } //移除最后一行 table.getLastRow().remove(); doc.save(path); long now = System.currentTimeMillis(); System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时 } }
2.效果