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

相关推荐
MY_TEUCK36 分钟前
【Java 后端】SpringBoot 登录认证与会话跟踪实战(JWT + Filter/Interceptor)
java·开发语言·spring boot
今天长肉了吗1 小时前
银行风控项目踩坑实录:指标跑了6小时,风险评分全挂了
java
随读手机1 小时前
多式联运信息交互平台完整方案(2026版)
java·ai·eclipse·云计算·区块链
许彰午2 小时前
03-二叉树——从递归遍历到非递归实现
java·算法
nj01282 小时前
Spring 循环依赖详解:三级缓存、早期引用、AOP 代理与懒加载
java·spring·缓存
野生技术架构师2 小时前
2026年最全Java面试题及答案汇总(建议收藏,面试前看这篇就够了)
java·开发语言·面试
一只叫煤球的猫3 小时前
ThreadForge 源码解读一:ThreadScope 如何把并发任务放进清晰边界?
java·面试·开源
洛_尘3 小时前
Python 5:使用库
java·前端·python
程序员小假4 小时前
HTTP3 性能更好,为什么内网微服务依然多用 HTTP2?HTTP2 内网优势是什么?
java·后端
Mr数据杨4 小时前
【Codex】用教案主体模块沉淀标准化教学设计内容
java·开发语言·django·codex·项目开发