Apache Poi Ppt 简明教程

Apache POI PPT - Images

在本章中,您将学习如何向 PPT 添加图像以及如何从中读取图像。

Adding Image

您可以使用 createPicture()XSLFSlide 方法向演示文稿添加图像。此方法以字节数组格式接受图像。因此,您需要创建要添加到演示文稿的图像的字节数组。

按照给定的步骤向演示文稿添加图像。使用 XMLSlideShow 创建一个空幻灯片,如下所示 −

XMLSlideShow ppt = new XMLSlideShow();

在其中使用 createSlide() 创建一个空演示文稿。

XSLFSlide slide = ppt.createSlide();

读取要添加的图像文件,并使用 IOUtils 类的 IOUtils.toByteArray() 将其转换为字节数组,如下所示 −

//reading an image
File image = new File("C://POIPPT//boy.jpg");

//converting it into a byte array
byte[] picture = IOUtils.toByteArray(new FileInputStream(image));

使用 addPicture() 将图像添加到演示文稿。此方法接受两个变量:要添加的图像的字节数组格式和表示图像文件格式的静态变量。 addPicture() 方法的用法如下所示 −

XSLFPictureData idx = ppt.addPicture(picture, XSLFPictureData.PICTURE_TYPE_PNG);

使用 createPicture() 将图像嵌入到幻灯片中,如下所示 −

XSLFPictureShape pic = slide.createPicture(idx);

下面给出了在演示文稿的幻灯片中添加图像的完整程序 −

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.sl.usermodel.PictureData.PictureType;
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 AddingImage {
   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//boy.jpg");

      //converting it into a byte array
      byte[] picture = IOUtils.toByteArray(new FileInputStream(image));

      //adding the image to the presentation
      XSLFPictureData idx = ppt.addPicture(picture, PictureType.PNG);

      //creating a slide with given picture on it
      XSLFPictureShape pic = slide.createPicture(idx);

      //creating a file object
      File file = new File("addingimage.pptx");
      FileOutputStream out = new FileOutputStream(file);

      //saving the changes to a file
      ppt.write(out);
      System.out.println("image added successfully");
      out.close();
   }
}

将上述 Java 代码另存为 AddingImage.java ,然后从命令提示符编译并执行,如下所示 −

$javac AddingImage.java
$java AddingImage

它将编译和执行以生成以下输出 −

reordering of the slides is done

带有新添加幻灯片的演示文稿(其中包含图像)如下所示 −

AddingImage

Reading Image

您可以使用 XMLSlideShow 类中的 getPictureData() 方法获取所有图片的数据。以下程序从演示文稿读取图像 −

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.sl.usermodel.PictureData.PictureType;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFPictureData;

public class Readingimage {
   public static void main(String args[]) throws IOException {
      //open an existing presentation
      File file = new File("addingimage.pptx");
      XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));

      //reading all the pictures in the presentation
      for(XSLFPictureData data : ppt.getPictureData()){
         byte[] bytes = data.getData();
         String fileName = data.getFileName();
         PictureType pictureFormat = data.getType();
         System.out.println("picture name: " + fileName);
         System.out.println("picture format: " + pictureFormat);
      }
      //saving the changes to a file
      FileOutputStream out = new FileOutputStream(file);
      ppt.write(out);
      out.close();
   }
}

将上述 Java 代码另存为 Readingimage.java ,然后从命令提示符编译并执行,如下所示 −

$javac Readingimage.java
$java Readingimage

它将编译和执行以生成以下输出 −

picture name: image1.png
picture format: 6