1. 程式人生 > >Aspose.words Java基於模板生成word之循環圖片

Aspose.words Java基於模板生成word之循環圖片

apt oid write posit lis 圖片路徑 位置 pri for

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("完成"); }

Aspose.words Java基於模板生成word之循環圖片