POI在word中插入图片

今天遇到一个新的任务:需要在一个word文件中插入一个流程图

一开始:使用默认方法插入流程图片但是发现默认图片总是嵌入布局无法展示完整

后来稍微调整了一下设置了一下段落格式 重新创建了一个新的段落去作为"容器"

复制代码
    public static void insertImageAtPlaceholder(XWPFDocument document, String placeholderPattern, String imagePath) throws IOException, InvalidFormatException {
        Pattern pattern = Pattern.compile(placeholderPattern);
        List<XWPFParagraph> paragraphs = document.getParagraphs();

        for (int i = 0; i < paragraphs.size(); i++) {
            XWPFParagraph paragraph = paragraphs.get(i);
            List<XWPFRun> runs = paragraph.getRuns();
            if (runs != null) {
                for (XWPFRun run : runs) {
                    String text = run.getText(0);
                    if (text != null) {
                        Matcher matcher = pattern.matcher(text);
                        if (matcher.find()) {
                            // 移除占位符
                            run.setText("", 0);

                            // 在当前段落后创建新段落
                            XWPFParagraph imageParagraph = document.insertNewParagraph(paragraph.getCTP().newCursor());

                            // 设置段落属性
                            imageParagraph.setAlignment(ParagraphAlignment.CENTER);
                            imageParagraph.setSpacingBefore(500);
                            imageParagraph.setSpacingAfter(500);

                            // 创建新的运行
                            XWPFRun newRun = imageParagraph.createRun();

                            try (FileInputStream is = new FileInputStream(imagePath)) {
                                // 获取图片实际尺寸
                                BufferedImage bimg = ImageIO.read(new File(imagePath));
                                int width = bimg.getWidth();
                                int height = bimg.getHeight();

                                // 计算合适的显示尺寸
                                double scaleFactor = 0.7;
                                int scaledWidth = (int) (width * scaleFactor);
                                int scaledHeight = (int) (height * scaleFactor);

                                // 插入图片
                                newRun.addPicture(
                                        is,
                                        Document.PICTURE_TYPE_PNG,
                                        imagePath,
                                        Units.pixelToEMU(scaledWidth),
                                        Units.pixelToEMU(scaledHeight)
                                );
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

最终结果:

相关推荐
风象南几秒前
SpringBoot配置属性热更新的轻量级实现
java·spring boot·后端
洛阳泰山1 分钟前
Spring Boot 整合 Nacos 实战教程:服务注册发现与配置中心详解
java·spring boot·后端·nacos
Y4090011 分钟前
C语言转Java语言,相同与相异之处
java·c语言·开发语言·笔记
YuTaoShao2 分钟前
【LeetCode 热题 100】994. 腐烂的橘子——BFS
java·linux·算法·leetcode·宽度优先
布朗克1682 分钟前
java常见的jvm内存分析工具
java·jvm·数据库
都叫我大帅哥1 小时前
深入浅出 Resilience4j:Java 微服务的“免疫系统”实战指南
java·spring cloud
Cao_Shixin攻城狮3 小时前
Flutter运行Android项目时显示java版本不兼容(Unsupported class file major version 65)的处理
android·java·flutter
Dcs6 小时前
还在用 Arrays.hashCode?Java 自己也能写出更快的版本!
java
fouryears_234178 小时前
Spring,Spring Boot 和 Spring MVC 的关系以及区别
java·spring boot·spring·mvc
阿葱(聪)8 小时前
java 在k8s中的部署流程
java·开发语言·docker·kubernetes