Apache POI PPT - 图片



本章将学习如何在PPT中添加图片以及如何从中读取图片。

添加图片

您可以使用XSLFSlidecreatePicture()方法将图片添加到演示文稿。此方法接受字节数组格式的图片。因此,您必须创建要添加到演示文稿的图片的字节数组。

按照以下步骤将图片添加到演示文稿。使用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

包含新添加的带图片幻灯片的演示文稿如下所示:

Adding 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 
广告