文章目录
前言
以xml的格式生成word,可以将图片的二进制数据保存在word中,无需考虑图片的存放位置。
一、制作freemarker模板
1、 准备模板
首先,新建一个docx,然后往word里粘贴一个图片,再将word另存为xml文件。
新建一个freemarker文件,如 word.ftl,将xml内容拷贝到word.ftl中。
2、修改模板
1、找到图片所在的Relationship ,修改成如下格式
<#list pictList as pict>
<Relationship Id="rId${pict_index+10}"
Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
Target="media/image${pict_index+10}.png"/>
</#list>
2、找到 <w:body>,写入图片的title和引用的Relationship
(1)title在
<w:t>${
pict.title}</w:t>
(2)Relationship 在
<a:blip r:embed="rId${pict_index+10}"/>
(3)完整的body如下
<w:body>
<#list pictList as pict >
<w:p>
<w:r>
<w:t>${
pict.title}</w:t>
</w:r>
<w:r>
<w:rPr>
<w:rFonts w:hint="eastAsia"/>
</w:rPr>
<w:t>:</w:t>
</w:r>
</w:p>
<w:p>
<w:r>
<w:drawing>
<wp:inline distT="0" distB="0" distL="0" distR="0">
<wp:extent cx="5274310" cy="2743835"/>
<wp:effectExtent l="0" t="0" r="2540" b="0"/>
<wp:docPr id="${pict_index+10}" name=

该博客介绍了使用Java和Freemarker以xml格式生成含图片的Word文档的方法。先制作Freemarker模板,包括准备和修改模板;接着编写后台代码,引入依赖、创建工具类方法和测试方法;最后展示了测试结果。
2939

被折叠的 条评论
为什么被折叠?



