- 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 - 幻灯片管理
完成本章后,您将能够对幻灯片进行删除、重新排序以及执行读写操作。
更换幻灯片
我们可以使用XMLSlideShow类的setPageSize()方法更改幻灯片的页面大小。
最初创建一个演示文稿,如下所示 -
File file = new File("C://POIPPT//Examples// TitleAndContentLayout.pptx"); //create presentation XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
使用XMLSlideShow类的getPageSize()方法获取当前幻灯片的大小。
java.awt.Dimension pgsize = ppt.getPageSize();
使用setPageSize()方法设置页面的大小。
ppt.setPageSize(new java.awt.Dimension(1024, 768));
下面给出了更改幻灯片大小的完整程序 -
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.XMLSlideShow; public class ChangingSlide { public static void main(String args[]) throws IOException { //create file object File file = new File("TitleAndContentLayout.pptx"); //create presentation XMLSlideShow ppt = new XMLSlideShow(); //getting the current page size java.awt.Dimension pgsize = ppt.getPageSize(); int pgw = pgsize.width; //slide width in points int pgh = pgsize.height; //slide height in points System.out.println("current page size of the PPT is:"); System.out.println("width :" + pgw); System.out.println("height :" + pgh); //set new page size ppt.setPageSize(new java.awt.Dimension(2048,1536)); //creating file object FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); System.out.println("slide size changed to given dimentions "); out.close(); } }
将上述 Java 代码保存为ChangingSlide.java,然后从命令提示符编译并执行它,如下所示 -
$javac ChangingSlide.java $java ChangingSlide
它将编译并执行以生成以下输出。
current page size of the presentation is : width :720 height :540 slide size changed to given dimensions
下面给出的是更改幻灯片大小之前演示文稿的快照 -
更改大小后幻灯片显示如下 -
重新排序幻灯片
您可以使用setSlideOrder()方法设置幻灯片顺序。下面给出的是设置幻灯片顺序的过程。
打开现有的 PPT 文档,如下所示 -
File file = new File("C://POIPPT//Examples//example1.pptx"); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
使用getSlides()方法获取幻灯片,如下所示 -
List<XSLFSlide> slides = ppt.getSlides();
从幻灯片数组中选择一张幻灯片,然后使用setSlideOrder()方法更改顺序,如下所示 -
//selecting the fourth slide XSLFSlide selectesdslide = slides.get(4); //bringing it to the top ppt.setSlideOrder(selectesdslide, 1);
下面给出的是对演示文稿中的幻灯片重新排序的完整程序 -
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; import org.apache.poi.xslf.usermodel.XMLSlideShow; import org.apache.poi.xslf.usermodel.XSLFSlide; public class ReorderSlide { public static void main(String args[]) throws IOException { //opening an existing presentation File file = new File("example1.pptx"); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); //get the slides List<XSLFSlide> slides = ppt.getSlides(); //selecting the fourth slide XSLFSlide selectesdslide = slides.get(13); //bringing it to the top ppt.setSlideOrder(selectesdslide, 0); //creating an file object FileOutputStream out = new FileOutputStream(file); //saving the changes to a file ppt.write(out); out.close(); } }
将上述 Java 代码保存为ReorderSlide.java,然后从命令提示符编译并执行它,如下所示 -
$javac ReorderSlide.java $java ReorderSlide
它将编译并执行以生成以下输出。
Reordering of the slides is done
下面给出的是重新排序幻灯片之前演示文稿的快照 -
重新排序幻灯片后,演示文稿如下所示。在这里,我们选择了带有图像的幻灯片并将其移动到顶部。
删除幻灯片
您可以使用removeSlide()方法删除幻灯片。请按照以下步骤删除幻灯片。
使用XMLSlideShow类打开现有演示文稿,如下所示 -
File file = new File("C://POIPPT//Examples//image.pptx"); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
使用removeSlide()方法删除所需的幻灯片。该方法接受一个整数参数。将要删除的幻灯片的索引传递给此方法。
ppt.removeSlide(1);
下面给出的是从演示文稿中删除幻灯片的程序 -
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.xslf.usermodel.XMLSlideShow; public class Deleteslide { public static void main(String args[]) throws IOException { //Opening an existing slide File file = new File("image.pptx"); XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file)); //deleting a slide ppt.removeSlide(1); //creating a file object FileOutputStream out = new FileOutputStream(file); //Saving the changes to the presentation ppt.write(out); out.close(); } }
将上述 Java 代码保存为Deleteslide.java,然后从命令提示符编译并执行它,如下所示 -
$javac Deleteslide.java $java Deleteslide
它将编译并执行以生成以下输出 -
reordering of the slides is done
下面的快照是删除幻灯片之前的演示文稿 -
删除幻灯片后,演示文稿如下所示 -