/*** 使用书签进行图片替换的核心代码 ***/
Document doc = new Document();
Paragraph paragraph = doc.createParagraph();
DocPicture picture = paragraph.appendPicture(imagePath);//imagePath图片路径
picture.setWidth(width);
picture.setHeight(height);
picture.setVerticalAlignment(ShapeVerticalAlignment.Center);
picture.setHorizontalAlignment(ShapeHorizontalAlignment.Center);
// 设置图片段落居中
paragraph.getFormat().setHorizontalAlignment(HorizontalAlignment.Center);
// 定位书签位置并将书签处的文本替换成图片
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(doc);
bookmarkNavigator.moveToBookmark(bookmark);//bookmark是Word中书签的名称
Paragraph oldParagraph = bookmarkNavigator.getCurrentBookmark().getBookmarkEnd().getOwnerParagraph();
DocumentObjectCollection childObjects = oldParagraph.getOwner().getChildObjects();
int indexOf = childObjects.indexOf(oldParagraph);
childObjects.remove(oldParagraph);
childObjects.insert(indexOf, paragraph);
Spire.Doc用来操作Word非常方便,提供的API文档也很全,不足的是文档中关于示例代码很少,平时开发Word报告时,查找起来比较麻烦。