Apache Poi Ppt 简明教程
Apache POI PPT - Images
在本章中,您将学习如何向 PPT 添加图像以及如何从中读取图像。
In this chapter, you will learn how to add an image to a PPT and how to read an image from it.
Adding Image
您可以使用 createPicture() 的 XSLFSlide 方法向演示文稿添加图像。此方法以字节数组格式接受图像。因此,您需要创建要添加到演示文稿的图像的字节数组。
You can add images to a presentation using the createPicture() method of XSLFSlide. This method accepts image in the form of byte array format. Therefore, you have to create a byte array of the image that is to be added to the presentation.
按照给定的步骤向演示文稿添加图像。使用 XMLSlideShow 创建一个空幻灯片,如下所示 −
Follow the given procedure to add an image to a presentation. Create an empty slideshow using XMLSlideShow as shown below −
XMLSlideShow ppt = new XMLSlideShow();
在其中使用 createSlide() 创建一个空演示文稿。
Create an empty presentation in it using createSlide().
XSLFSlide slide = ppt.createSlide();
读取要添加的图像文件,并使用 IOUtils 类的 IOUtils.toByteArray() 将其转换为字节数组,如下所示 −
Read the image file that is to be added and convert it into byte array using IOUtils.toByteArray() of the IOUtils class as shown below −
//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() 方法的用法如下所示 −
Add the image to the presentation using addPicture(). This method accepts two variables: byte array format of the image that is to be added and the static variable representing the file format of the image. The usage of the addPicture() method is shown below −
XSLFPictureData idx = ppt.addPicture(picture, XSLFPictureData.PICTURE_TYPE_PNG);
使用 createPicture() 将图像嵌入到幻灯片中,如下所示 −
Embed the image to the slide using createPicture() as shown below −
XSLFPictureShape pic = slide.createPicture(idx);
下面给出了在演示文稿的幻灯片中添加图像的完整程序 −
Given below is the complete program to add an image to the slide in a presentation −
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 ,然后从命令提示符编译并执行,如下所示 −
Save the above Java code as AddingImage.java, and then compile and execute it from the command prompt as follows −
$javac AddingImage.java
$java AddingImage
它将编译和执行以生成以下输出 −
It will compile and execute to generate the following output −
reordering of the slides is done
带有新添加幻灯片的演示文稿(其中包含图像)如下所示 −
The presentation with the newly added slide with image appears as follows −
Reading Image
您可以使用 XMLSlideShow 类中的 getPictureData() 方法获取所有图片的数据。以下程序从演示文稿读取图像 −
You can get the data of all the pictures using the getPictureData() method of the XMLSlideShow class. The following program reads the images from a presentation −
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 ,然后从命令提示符编译并执行,如下所示 −
Save the above Java code as Readingimage.java, and then compile and execute it from the command prompt as follows −
$javac Readingimage.java
$java Readingimage
它将编译和执行以生成以下输出 −
It will compile and execute to generate the following output −
picture name: image1.png
picture format: 6