java实现word末尾添加图片的两种方式

java实现word末尾添加图片的两种方式:

  1. jdk1.8之前版本

    public static void addImagesToWordEnd(String inputFilePath, String outputFilePath,
    String[] imagePaths, float imgWidth, float imgHeight) {
    // 验证图片路径数量
    if (imagePaths == null || imagePaths.length < 3) {
    throw new IllegalArgumentException("必须提供三张图片路径");
    }

    复制代码
         // 创建并加载文档
         Document doc = new Document();
         doc.loadFromFile(inputFilePath);
    
         // 获取文档最后一个节
         Section lastSection = doc.getLastSection();
    
         // 创建图片展示段落
         Paragraph imgParagraph = lastSection.addParagraph();
         imgParagraph.getFormat().setHorizontalAlignment(com.spire.doc.documents.HorizontalAlignment.Left);
    
         // 添加三组文字+图片
         String[] captions = {"一审:", "二审:", "终审:"};
    
         for (int i = 0; i < 3; i++) {
             // 添加文字标签
             TextRange captionText = new TextRange(doc);
             captionText.setText(captions[i]);
             imgParagraph.getChildObjects().add(captionText);
    
             // 添加图片
             DocPicture picture = new DocPicture(doc);
             picture.loadImage(imagePaths[i]);
             picture.setWidth(imgWidth);
             picture.setHeight(imgHeight);
             picture.setTextWrappingStyle(TextWrappingStyle.Inline);
             imgParagraph.getChildObjects().add(picture);
    
             // 添加空格分隔(最后一组不加空格)
             if (i < 2) {
                 TextRange space = new TextRange(doc);
                 space.setText("  ");  // 使用两个空格增加间距
                 imgParagraph.getChildObjects().add(space);
             }
         }
    
         // 保存文档
         doc.saveToFile(outputFilePath, FileFormat.Docx);
     }

相关jar包:Spire.Doc.jar

  1. jdk1.8版本之后
java 复制代码
public static void appendImagesToDocx(String sourceDocxPath, String targetDocxPath, String[] imagePaths)
            throws IOException, InvalidFormatException {

        FileInputStream docStream = null;
        FileOutputStream out = null;
        XWPFDocument doc = null;

        try {
            // 1. 打开现有文档
            docStream = new FileInputStream(sourceDocxPath);
            doc = new XWPFDocument(docStream);

            // 2. 创建段落并设置居中
            XWPFParagraph paragraph = doc.createParagraph();
            paragraph.setAlignment(ParagraphAlignment.CENTER); // 整体内容居中

            // 文字描述与图片对应关系
            String[] descriptions = {"部门领导:", "学校审核:", "校领导:"};

            for (int i = 0; i < imagePaths.length; i++) {
                String imgPath = imagePaths[i];
                String description = descriptions[i];

                // 创建 Run 添加文字描述
                XWPFRun runText = paragraph.createRun();
                runText.setText(description);

                // 创建 Run 插入图片
                XWPFRun runImage = paragraph.createRun();

                // 插入图片(自动识别图片类型)
                int pictureType = getPictureType(imgPath);

                // 使用 FileInputStream 直接插入图片
                FileInputStream imageStream = new FileInputStream(imgPath);

                // 设置图片宽高(使用 EMUs 单位)
                int width = (int) (5 * Units.EMU_PER_CENTIMETER); // 5 厘米宽
                int height = (int) (3 * Units.EMU_PER_CENTIMETER); // 3 厘米高

                runImage.addPicture(imageStream, pictureType, imgPath, width, height);

                // 如果不是最后一张图片,则添加空格分隔
                if (i < imagePaths.length - 1) {
                    XWPFRun runComma = paragraph.createRun();
                    runComma.setText("  ");
                }
            }

            // 3. 保存文档
            out = new FileOutputStream(targetDocxPath);
            doc.write(out);

        } finally {
            // 关闭资源(JDK 1.6 不支持 try-with-resources)
            closeQuietly(out);
            closeQuietly(docStream);
        }
    }

相关依赖: poi5.2.3、poi-ooxm5.2.3

相关推荐
不爱编程的小九九几秒前
小九源码-springboot097-java付费自习室管理系统
java·开发语言·spring boot
极智-99610 分钟前
word怎么自动生成目录?【图文详解】word目录自动生成?怎么自动生成目录和页码?
word·word使用技巧·word怎么自动生成目录·word文档目录·怎么自动生成目录和页码·word目录自动生成·word文档目录生成
独自破碎E22 分钟前
LeetCode 381: O(1) 时间插入、删除和获取随机元素 - 允许重复
java·算法·leetcode
程语有云24 分钟前
生产事故-Caffeine缓存误用之临下班的救赎
java·缓存·caffeine·阻塞·log·生产事故
Miraitowa_cheems1 小时前
LeetCode算法日记 - Day 81: 最大子数组和
java·数据结构·算法·leetcode·决策树·职场和发展·深度优先
CodeCraft Studio1 小时前
国产化Word处理控件Spire.Doc教程:用Java实现TXT文本与Word互转的完整教程
java·c#·word·spire.doc·word文档转换·txt转word·word转txt
徐子童1 小时前
数据结构---优先级队列(堆)
java·数据结构·面试题·优先级队列··topk问题
滑水滑成滑头1 小时前
**标题:发散创新:智能交通系统的深度探究与实现**摘要:本文将详细
java·人工智能·python
siriuuus1 小时前
Maven 核心概念及生命周期
java·maven
闭着眼睛学算法1 小时前
【双机位A卷】华为OD笔试之【哈希表】双机位A-跳房子I【Py/Java/C++/C/JS/Go六种语言】【欧弟算法】全网注释最详细分类最全的华子OD真题题解
java·c语言·c++·python·算法·华为od·散列表