前提:
1.知道如何创建ftl模板
2.知道ftl的指令及语法
3.知道如何用java将ftl文件生成word并添加数据
4.知道如何将图片转为base64字符串
内容:
1.在ftl模板中引入多个图片
<#if imageList?? && (imageList?size) > 0><#list imageList as image><pkg:part pkg:name="/word/media/image_${image_index}.jpeg"pkg:contentType="image/jpeg"><pkg:binaryData>${image}</pkg:binaryData></pkg:part></#list></#if>
在ftl模板的下方添加<pkg:part>标签,通过该标签引入图片,代码如上所示。其中:imageList为List<String>的对象,里面存放图片的base64字符串,pkg:name属性的值要唯一;<pkg:binaryData>标签内不能有空格和换行。
2.展示图片
在ftl模板上方的该标签<pkg:part>内添加以下标签用于其他位置引用该图片,
<#if imageList?? && (imageList?size) > 0><#list imageList as image><Relationship Id="imageId_${image_index}"Type="/officeDocument//relationships/image"arget="media/image_${image_index}.jpeg" /></#list></#if>
其中arget属性中的值必须和图片标签的属性pag:name相似(少了个'/word/'),Id属性必须唯一。
展示:
<#if imageList?? && (imageList?size) > 0><#list imageList as image><w:pict><v:shape id="_x0000_i1026_${image_index}" o:spt="75" alt="图片"type="#_x0000_t75"style="height:21.15pt;width:42.3pt;" filled="f"o:preferrelative="t" stroked="f"coordsize="21600,21600"><v:path /><v:fill on="f" focussize="0,0" /><v:stroke on="f" /><v:imagedata r:id="imageId_${image_index}" o:title="图" /><o:lock v:ext="edit" aspectratio="t" /><w10:wrap type="none" /><w10:anchorlock /></v:shape></w:pict></#list></#if>
通过在需要的位置添加<w:pict>标签进行图片展示,<v:imagedata>引用对应的图片。其中<v:imagedata>的属性r:id需要和<Relationship>的属性Id对应。