📅  最后修改于: 2020-11-18 08:51:39             🧑  作者: Mango
在本章中,您将学习如何将图像添加到PPT中以及如何从中读取图像。
您可以将图片用XSLFSlide的createPicture()方法的介绍。此方法接受字节数组格式的图像。因此,您必须创建要添加到演示文稿中的图像的字节数组。
按照给定的步骤将图像添加到演示文稿。使用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()方法的用法如下所示-
int 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.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
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("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
带有新添加的带有图像的幻灯片的演示文稿如下所示-
您可以使用XMLSlideShow类的getAllPictures()方法获取所有图片的数据。以下程序从演示文稿中读取图像-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
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.getAllPictures()){
byte[] bytes = data.getData();
String fileName = data.getFileName();
int pictureFormat = data.getPictureType();
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