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

最终结果:

相关推荐
卷心菜好6啊7 分钟前
特辣的海藻!2
java
心态与习惯10 分钟前
mac 下 java 调用 gurobi 不能加载 jar
java·jar·mac·cplex·gurobi
he2581911 分钟前
centOS 7.9 安装JDK MYSQL
java·mysql·centos
找了一圈尾巴23 分钟前
Spring Boot 日志管理(官网文档解读)
java·spring boot
升讯威在线客服系统23 分钟前
如何通过 Docker 在没有域名的情况下快速上线客服系统
java·运维·前端·python·docker·容器·.net
s:1031 小时前
【框架】参考 Spring Security 安全框架设计出,轻量化高可扩展的身份认证与授权架构
java·开发语言
不学能干嘛2 小时前
写大论文的word版本格式整理,实现自动生成目录、参考文献序号、公式序号、图表序号
word
南山十一少4 小时前
Spring Security+JWT+Redis实现项目级前后端分离认证授权
java·spring·bootstrap
427724006 小时前
IDEA使用git不提示账号密码登录,而是输入token问题解决
java·git·intellij-idea
chengooooooo6 小时前
苍穹外卖day8 地址上传 用户下单 订单支付
java·服务器·数据库