POI替换Word图片

0

之前做了一个Word在线编辑导出的功能,里面替换图片是把文字替换为图片,原因也是之前的Word的头像都是简单的四边形,没有特殊效果。
但是最近的模板是圆图,而且有边框和阴影效果:

Word模板

之前替换文本的文章:http://blog.csdn.net/webrobot/article/details/25989295

那么现在就只能去替换图片了,但是网上找了找也没找到,POI的方法也找了一下,没有替换图片的方法。
然而在我不懈努力之下,最后终于是找到了解决办法。

首先我们把一个Word的文档用7z解压出来或者把后缀改为zip解压都可以,可以得到以下的文件:

Word解压

word/document.xml这个文件里面就是我们编辑的Word的文本,图片资源在word/media这个目录下面,那么Word里面的图片哪里定义的呢?
就在:word/_rels/document.xml.rels这个文件里面,打开后我们可以看到:

<?xml version="1.0" encoding="UTF-8"?>
 
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/numbering" Target="numbering.xml"/>
  <Relationship Id="rId10" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image4.png"/>
  <Relationship Id="rId11" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image5.png"/>
  <Relationship Id="rId12" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image6.png"/>
  <Relationship Id="rId13" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/fontTable" Target="fontTable.xml"/>
  <Relationship Id="rId14" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme" Target="theme/theme1.xml"/>
  <Relationship Id="rId15" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image7.jpeg"/>
  <Relationship Id="rId2" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles" Target="styles.xml"/>
  <Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings" Target="settings.xml"/>
  <Relationship Id="rId4" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/webSettings" Target="webSettings.xml"/>
  <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/footnotes" Target="footnotes.xml"/>
  <Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/endnotes" Target="endnotes.xml"/>
  <Relationship Id="rId7" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image7.jpeg"/>
  <Relationship Id="rId8" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image2.png"/>
  <Relationship Id="rId9" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image" Target="media/image3.png"/>
</Relationships>

document.xml里引用这里的id然后通过target找到图片,那么我们就可以替换图片了,替换的代码如下:

// 这一步就是把图片放到word/media里面,参数分别是:图片字节流和图片的类型,上面的文章都有,返回值就是上面资源引用的id
String rid = doc.addPictureData(imagebis, imageType);
// 先删除旧的id,否者会提示错误
doc.getPackagePart().removeRelationship("rId7");
// 然后加入资源引用,参数:图片地址,引用类型以及id
doc.getPackagePart().addRelationship(doc.getRelationById(rid).getPackageRelationship().getTargetURI(), TargetMode.INTERNAL, XWPFRelation.IMAGES.getRelation(), "rId7");

还有一个addExternalRelationship的方法参数和addRelationship差不多,第二个直接填写上面XML的type内容就可以了。
但是这个只引用外部图片,我估计应该就是使用E://acgist.png这样的图片路径,而非打包到docx里面的图片。

但是这里有一个缺点就是必须要知道需要替换图片的ID,否者是不能替换成功的。

20220602批注
其实大部分文件都是压缩文件,都可以使用7z打开。