Java 示例 - 将图像添加到幻灯片

  • 问题描述

    如何使用java将图像添加到PPT中的幻灯片。
  • 解决方案

    以下是使用 java 将图像添加到 PPT 中的幻灯片的程序。
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    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;
    public class AddingImageToPPT {
       public static void main(String args[]) throws IOException {
          //creating a presentation
          XMLSlideShow ppt = new XMLSlideShow();
          
          //creating a slide in it
          XSLFSlide slide = ppt.createSlide();
          //reading an image
          File image = new File("C:/poippt/cat.jpg");
          //converting it into a byte array
          byte[] picture = IOUtils.toByteArray(new FileInputStream(image));
          //adding the image to the presentation
          int idx = ppt.addPicture(picture, XSLFPictureData.PICTURE_TYPE_PNG);
          //creating a slide with given picture on it
          XSLFPictureShape pic = slide.createPicture(idx);
          //creating a file object
          File file = new File("C:/poippt/AddingimageToPPT.pptx");
          FileOutputStream out = new FileOutputStream(file);
          //saving the changes to a file
          ppt.write(out);
          
          System.out.println("image added successfully");
          out.close();
       }
    }
    
  • 输入

    看图片
  • 输出

    添加图像