Aspose.words Java基于模板生成word之循环图片

1.新建一个word文档

2.给插入图片的地方设置书签

 3,设置书签

二,项目

1,2步的引入依赖以及加载授权文件同上一篇

3,获取图片路径插入到word中并生成新的word文档

新文档中,每行显示两张图片

public static void main(String[] args) throws Exception {
        // 验证License
        if (!getLicense()) {
            return;
        }
        //模板word
        String template = "E:\test\temp.docx";
        //目标word
        String destdoc = "E:\test\edit.docx";
        //定义文档接口
        Document doc = new Document(template);
        //图片路径
        List<String> pathList = new ArrayList<>();
        pathList.add("E:\test\image1.jpg");
        pathList.add("E:\test\image2.jpg");
        pathList.add("E:\test\image3.jpg");
        pathList.add("E:\test\image4.jpg");
        pathList.add("E:\test\image5.jpg");

        DocumentBuilder builder = new DocumentBuilder(doc);
     //定位到指定位置 builder.moveToBookmark(
"pics"); double length = pathList.size(); for (int i = 0; i < Math.ceil(length/2.0); i++){ for (int j = 0; j < 2; j++){ int index = i*2 + j; if (index < pathList.size()){ if (i == 0){ if (j == 0){ builder.insertImage(pathList.get(index), RelativeVerticalPosition.MARGIN,1, RelativeHorizontalPosition.MARGIN,130,200,220,WrapType.INLINE); }else { builder.insertImage(pathList.get(index),RelativeVerticalPosition.MARGIN,230,RelativeHorizontalPosition.MARGIN,130,200,220,WrapType.INLINE); } }else { if (j == 0){ builder.insertImage(pathList.get(index),RelativeVerticalPosition.MARGIN,1,RelativeHorizontalPosition.MARGIN,(130+220*i+10*i),200,220,WrapType.INLINE); }else { builder.insertImage(pathList.get(index),RelativeVerticalPosition.MARGIN,230,RelativeHorizontalPosition.MARGIN,(130+220*i+10*i),200,220,WrapType.INLINE); } } } builder.write(" "); } builder.writeln(); } //调用接口 doc.save(destdoc); System.out.println("完成"); }
原文地址:https://www.cnblogs.com/cailijuan/p/9383012.html