想要的效果如下
// 创建一个Document对象
Document document = new Document();
// 加载文档数据
document.loadFromFile("input.docx");
// 获取文档的第一个节对象
Section section = document.getSections().get(0);
// 获取节中的第一个表格对象
Table table = section.getTables().get(0);
// 获取表格中位于第3行第4列的单元格对象
TableCell cell = table.get(2, 3);
// 获取单元格中的第一个段落对象
Paragraph paragraph = cell.getParagraphs().get(0);
// 创建一个StructureDocumentTagInline对象
StructureDocumentTagInline picControl = new StructureDocumentTagInline(document);
// 设置SDT类型为Picture,并设置外观为Tags
picControl.getSDTProperties().setSDTType(SdtType.Picture);
picControl.getSDTProperties().setAppearance(SdtAppearance.Bounding_Box);
// 创建一个DocPicture对象
//DocPicture picture = new DocPicture(document);
// 加载图像
//picture.loadImage("input.jpg");
// 设置图像的宽度和高度
//picture.setWidth(50);
//picture.setHeight(50);
// 将图像添加到SDT内容中
//picControl.getSDTContent().getChildObjects().add(picture);
// 将图片内容控件添加到段落中
paragraph.getChildObjects().add(picControl);
// 保存文件
document.saveToFile("output.docx");// 创建一个新的文档对象
Document document = new Document();
// 加载文档数据
document.loadFromFile("input.docx");
// 获取文档的第一个节对象
Section section = document.getSections().get(0);
// 创建一个DocPicture对象
DocPicture picture = new DocPicture(document);
// 加载名为"image.png"的图像
picture.loadImage("image.png");
// 创建一个StructureDocumentTagInline对象作为图片容器
StructureDocumentTagInline picControl = new StructureDocumentTagInline(document);
// 设置SDT标签为"pic1"
picControl.getSDTProperties().setTag("pic1");
// 设置SDT类型为Picture
picControl.getSDTProperties().setSDTType(SdtType.Picture);
// 设置SDT控制属性为SdtPicture
picControl.getSDTProperties().setControlProperties(new SdtPicture());
// 设置外观为Bounding_Box
picControl.getSDTProperties().setAppearance(SdtAppearance.Bounding_Box);
// 设置显示占位符
picControl.getSDTProperties().isShowingPlaceHolder(true);
// 将图像添加到SDT内容中
picControl.getSDTContent().getChildObjects().add(picture);
// 获取表格中的第一个表格对象
Table table = section.getTables().get(0);
// 获取表格的第一行第一列的单元格对象
Paragraph paragraph1 = table.getRows().get(0).getCells().get(0).addParagraph();
// 将图片容器添加到段落中
paragraph1.getChildObjects().add(picControl);
// 保存文档
document.saveToFile("output.docx");