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

最终结果:

相关推荐
TT哇37 分钟前
【Java EE初阶】计算机是如何⼯作的
java·redis·java-ee
Fireworkitte7 小时前
Apache POI 详解 - Java 操作 Excel/Word/PPT
java·apache·excel
weixin-a153003083167 小时前
【playwright篇】教程(十七)[html元素知识]
java·前端·html
DCTANT8 小时前
【原创】国产化适配-全量迁移MySQL数据到OpenGauss数据库
java·数据库·spring boot·mysql·opengauss
Touper.8 小时前
SpringBoot -- 自动配置原理
java·spring boot·后端
黄雪超8 小时前
JVM——函数式语法糖:如何使用Function、Stream来编写函数式程序?
java·开发语言·jvm
ThetaarSofVenice8 小时前
对象的finalization机制Test
java·开发语言·jvm
望获linux10 小时前
【实时Linux实战系列】CPU 隔离与屏蔽技术
java·linux·运维·服务器·操作系统·开源软件·嵌入式软件
JosieBook10 小时前
【Java编程动手学】使用IDEA创建第一个HelloJava程序
java·开发语言·intellij-idea
Thomas_YXQ10 小时前
Unity3D DOTS场景流式加载技术
java·开发语言·unity