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

最终结果:

相关推荐
zzb158011 分钟前
Agent案例-智能文档问答助手
java·人工智能·笔记·python
dd向上19 分钟前
【计算机毕设/课设】在职全栈开发工程师接单:Java(SpringBoot+Vue)/小程序/C++(Qt/MFC) 定制与辅导
java·spring boot·课程设计
ckm紫韵21 分钟前
Maven搭建私服Nexus教程
java·maven·nexus
故以往之不谏22 分钟前
JAVA--类和对象4.1--构造方法基础
java·开发语言·javascript
014-code23 分钟前
Java Optional 那些被忽略的用法
java·数据库·javase
A 小码农25 分钟前
亲测AI智能小助手-IDEA中使用腾讯混元大模型
java·人工智能·intellij-idea
eggwyw26 分钟前
redis 使用
java
ERBU DISH28 分钟前
ChatGLM2-6B模型推理流程和模型架构详解
java
BullSmall33 分钟前
IntelliJ IDEA 安装与环境配置指南(2026 最新)
java·ide·intellij-idea
Javatutouhouduan36 分钟前
SQL优化从入门到精通!
java·数据库·mysql·sql优化·java面试·后端开发·java程序员