使用 POI 向word文档段落中间插入图片

    /**
     * 计算图片宽高
     * @param picPath
     * @return
     */
    public static int[] getPictureWidthAndHeght(String picPath) {
        int[] sizeArray = new int[2];
        BufferedImage bf = null;
        try {
            bf = ImageIO.read(new File(picPath));
            int width = bf.getWidth();
            int height = bf.getHeight();
            int widthEMU = Units.toEMU(width);
            int heightEMU = Units.toEMU(height);

            if (widthEMU / 360000 > 16) {
                NumberFormat f = NumberFormat.getNumberInstance();
                f.setMaximumFractionDigits(0);
                f.setRoundingMode(RoundingMode.UP);
                //获取图片的EMU和图片在文档中最大EMU的比例值
                double d = widthEMU / (360000 * 14d);
                //根据比例值计算得出适合文档的最大EMU数值
                widthEMU = Integer.parseInt(f.format(widthEMU / d).replace(",", ""));
                heightEMU = Integer.parseInt(f.format(heightEMU / d).replace(",", ""));
                sizeArray[0] = widthEMU;
                sizeArray[1] = heightEMU;
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return sizeArray;
    }

word中插入图片

    public static void main(String[] args) throws IOException {

        String docName = "段落中间插入图片.docx";
        String wordFilePath = "E:\\word\\";
        String wordOutPutPath = "E:\\word\\output\\";
        // 图片地址
        String picturePath = "E:\\word\\abc.png";
        InputStream is = null;
        FileOutputStream fos = null;
        InputStream pis = null;
        int parentParagraphNum = 3;     // 指定图片插入的段落
        try {
            is = new FileInputStream(wordFilePath + docName);
            pis = new FileInputStream(picturePath);
            XWPFDocument doc = new XWPFDocument(is);
            List<XWPFParagraph> paragraphs = doc.getParagraphs();

            for (int i = 0; i < paragraphs.size(); i++) {
                if (i == parentParagraphNum) {
                    XWPFParagraph lastParagraph = paragraphs.get(i);
                    XmlCursor cursor = lastParagraph.getCTP().newCursor();
                    XWPFParagraph newPara = doc.insertNewParagraph(cursor);
                    XWPFRun newParaRun = newPara.createRun();
                    newParaRun.addBreak();
                    newParaRun.addBreak();
                    newParaRun.addBreak();
                    newParaRun.addBreak();
                    newParaRun.addBreak();
                    newParaRun.addBreak();
                    newParaRun.addBreak();
                    newParaRun.addBreak();
                    int[] sizeArray = WordUtil.getPictureWidthAndHeght(picturePath);
                    newParaRun.addPicture(pis, XWPFDocument.PICTURE_TYPE_PNG, picturePath, sizeArray[0], sizeArray[1]);
                }
            }

            fos = new FileOutputStream(wordOutPutPath + docName);
            doc.write(fos);
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                is.close();
            }
            if (fos != null) {
                fos.close();
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值