Apache POI PPT 设置图像

要在幻灯片中设置图像,我们可以使用 POI API 的 addPicture() 和 createPicture() 方法。在以下示例中,我们使用这两种方法在文档中写入图像。

Apache POI PPT 幻灯片设置图像示例

package com.yiidian;

import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;

import java.io.*;

public class ReOrderSlidesExample {
    public static void main(String[] args) throws FileNotFoundException, IOException {
        XMLSlideShow ppt = new XMLSlideShow();
        try (OutputStream os = new FileOutputStream("yiidian.pptx")) {
            XSLFSlide slide = ppt.createSlide();
            byte[] pictureData = IOUtils.toByteArray(new FileInputStream("logo.png"));
            XSLFPictureData pd = ppt.addPicture(pictureData, XSLFPictureData.PictureType.PNG);
            XSLFPictureShape pic = slide.createPicture(pd);
            ppt.write(os);  
        }catch(Exception e) {  
            System.out.println(e);  
        }  
    }  
}  

输出结果为:

热门文章

优秀文章