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;
                        }
                    }
                }
            }
        }
    }

最终结果:

相关推荐
君顾18 分钟前
外卖CPS系统开发实战指南:从架构设计到部署全流程解析
java·开发语言·外卖
奇牙coding1231 小时前
GPT-5.5 升级 GPT-5.6 接入指南:流式调用配置与常见问题
java·网络·gpt·ai
老刘的望远镜1 小时前
AgentScope Java 从零(03):Agent 的记性默认全开,我在第 50 轮翻了车
java·开发语言·javascript
yxlalm1 小时前
5.3解决超卖问题
java·spring boot·超卖
李少兄2 小时前
Java 中只有值传递吗?
java·开发语言·python
名字还没想好☜2 小时前
Spring @EventListener 事件驱动解耦实战:同步转异步、事务绑定与顺序控制
java·数据库·后端·python·spring
一木 之林2 小时前
插件、MCP、Skill 的区别?
java·c++·人工智能
小刘在重生~2 小时前
面试重点:final、finalize、finally 三者区别
java
边境悍匪3 小时前
蜗牛学苑 Java 智能体学习 Day39|SpringAI 会话存储、Function‑Call 工具调用、MCP 思维导图复盘
java·开发语言·spring boot·学习·阿里云
letisgo53 小时前
JAVA 高级进阶02篇《并发编程三板斧:JMM、CAS与AQS的源码级拆解》
java·面试·并发编程·aqs·jmm