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

最终结果:

相关推荐
李子园的李2 分钟前
Java函数式接口——渐进式学习
java
running up6 分钟前
Spring Bean生命周期- BeanDefinition 加载与 BeanFactoryPostProcessor BeanPostProcessor
java·后端·spring
222you15 分钟前
Java线程的三种创建方式
java·开发语言
脸大是真的好~21 分钟前
计算机408基础相关面试题-备用,不推荐
java
小费的部落22 分钟前
Excel 在Sheet3中 匹配Sheet1的A列和Sheet2的A列并处理空内容
java·前端·excel
咘噜biu23 分钟前
多租户动态数据源插件dynamic-datasource简介
java·mybatisplus·动态数据源·多租户
漫漫求25 分钟前
Java内存模型【JMM】、JVM内存模型
java·开发语言·jvm
原来是好奇心25 分钟前
深入Spring Boot源码(五):外部化配置与Profile机制深度解析
java·源码·springboot
IT界的奇葩26 分钟前
OAuth2 单点登录流程图
java·流程图·oauth2·单点登录·sso
ZHang......1 小时前
LeetCode 1114. 按序打印
java·开发语言·算法