继续潜水

导航

 

相关文章:

Xdocreport 生成docx文件 (一)  

Xdocreport 生成docx文件 (二)  

Xdocreport 生成docx文件 (三)  

对文件中添加一张可以替换的图片

插入一个图片,选中图片,插入=>书签=>添加 img

 

 代码:

 1     public static void main(String[] args) throws Exception {
 2         only_name_img();
 3     }
 4     //实现类
 5     public static void only_name_img() throws Exception {
 6         //1.通过freemarker模板引擎加载文档,并缓存到registry中
 7         InputStream in = new FileInputStream(BASE_PATH + "only_name_img.docx");
 8         IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Freemarker);
 9         //2.设置填充字段、填充类以及是否为list。
10         FieldsMetadata fieldsMetadata = report.createFieldsMetadata();
11         IContext context = report.createContext();
12         context.put("name", "踢足球");
13         context.put("sex", "男");
14         /**********新增img************/
15         //添加图片
16         IImageProvider img = new FileImageProvider(
17                 new File("/Users/tongzuqi/Pictures/timg (10).jpeg"),
18                 true);
19         //设置图片大小
20         img.setSize(70f, 50f);
21         context.put("img", img);
22         //特殊字符
23         fieldsMetadata.addFieldAsImage("img");
24         /**********新增img************/
25         OutputStream out = new FileOutputStream( new File(BASE_PATH + "only_name_img-out.docx"));
26         report.setFieldsMetadata(fieldsMetadata);
27         report.process(context, out);
28     }

 

 生成后的文件结果为:

 

posted on 2022-10-19 10:34  继续潜水  阅读(873)  评论(0编辑  收藏  举报