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

相关推荐
平生不喜凡桃李7 分钟前
浅谈 Linux 中 namespace 相关系统调用
java·linux·服务器
zb2006412013 分钟前
CVE-2024-38819:Spring 框架路径遍历 PoC 漏洞复现
java·后端·spring
2401_8955213423 分钟前
spring-ai 下载不了依赖spring-ai-openai-spring-boot-starter
java·人工智能·spring
何仙鸟1 小时前
GarmageSet下载和处理
java·开发语言
wefly20171 小时前
免安装!m3u8live.cn在线 M3U8 播放器,小白也能快速上手
java·开发语言·python·json·php·m3u8·m3u8在线转换
yuweiade1 小时前
springboot和springframework版本依赖关系
java·spring boot·后端
ywf12151 小时前
springboot设置多环境配置文件
java·spring boot·后端
小马爱打代码1 小时前
SpringBoot + 消息生产链路追踪 + 耗时分析:从创建到发送,全链路性能可视化
java·spring boot·后端
jessecyj2 小时前
Spring boot整合quartz方法
java·前端·spring boot
苦瓜小生2 小时前
【前端】|【js手撕】经典高频面试题:手写实现function.call、apply、bind
java·前端·javascript