- Apache POI PPT Tutorial
- Apache POI PPT - Home
- Apache POI PPT - Overview
- Apache POI PPT - Java API Flavors
- Apache POI PPT - Installation
- Apache POI PPT - Classes & Methods
- Apache POI PPT - Presentation
- Apache POI PPT - Slide Layouts
- Apache POI PPT - Slide Management
- Apache POI PPT - Images
- Apache POI PPT - Creating Hyperlinks
- Apache POI PPT - Reading Shapes
- Apache POI PPT - Formatting Text
- Apache POI PPT - Merging
- Apache POI PPT - PPT to Image
- Apache POI PPT Resources
- Apache POI PPT - Quick Guide
- Apache POI PPT - Useful Resources
- Apache POI PPT - Discussion
Apache POI PPT - 图像
在本章中,您将学习如何向 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()方法的用法如下所示 -
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
新添加的带有图像的幻灯片的演示文稿如下所示 -
读图
您可以使用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