Apache Poi Ppt 简明教程
Apache POI PPT - Slide Management
完成本章后,你将能够删除、重新排序并在幻灯片上执行读写操作。
After completing this chapter, you will be able to delete, reorder, and perform read and write operations on a slide.
Changing a Slide
我们可以使用 setPageSize() 类使用 XMLSlideShow 方法更改幻灯片的页面大小。
We can change the page size of a slide using the setPageSize() method of the XMLSlideShow class.
最初创建一个演示文稿,如下所示 −
Initially create a presentation as shown below −
File file = new File("C://POIPPT//Examples// TitleAndContentLayout.pptx");
//create presentation
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
使用 getPageSize() 方法从 XMLSlideShow 类中获取当前幻灯片的大小。
Get the size of the current slide using the getPageSize() method of the XMLSlideShow class.
java.awt.Dimension pgsize = ppt.getPageSize();
使用 setPageSize() 方法设置页面大小。
Set the size of the page using the setPageSize() method.
ppt.setPageSize(new java.awt.Dimension(1024, 768));
完整的更改幻灯片大小的程序如下 -
The complete program for changing the size of a slide is given below −
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 ,然后按照以下步骤从命令提示符处进行编译和执行 -
Save the above Java code as ChangingSlide.java, and then compile and execute it from the command prompt as follows −
$javac ChangingSlide.java
$java ChangingSlide
它会编译并执行以生成以下输出。
It will compile and execute to generate the following output.
current page size of the presentation is :
width :720
height :540
slide size changed to given dimensions
以下是更改幻灯片大小之前的演示文稿的快照 -
Given below is the snapshot of the presentation before changing the slide size −
更改大小后,幻灯片如下所示 -
The slide appears as follows after changing its size −
Reordering Slides
您可以使用 setSlideOrder() 方法设置幻灯片顺序。下面是设置幻灯片顺序的步骤。
You can set the slide order using the setSlideOrder() method. Given below is the procedure to set the order of the slides.
如以下所示打开现有的 PPT 文档 -
Open an existing PPT document as shown below −
File file = new File("C://POIPPT//Examples//example1.pptx");
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
使用 getSlides() 方法获取幻灯片,如下所示 -
Get the slides using the getSlides() method as shown below −
List<XSLFSlide> slides = ppt.getSlides();
从幻灯片数组中选择一个幻灯片,并使用 setSlideOrder() 方法更改顺序,如下所示 -
Select a slide from the array of the slides, and change the order using the setSlideOrder() method as shown below −
//selecting the fourth slide
XSLFSlide selectesdslide = slides.get(4);
//bringing it to the top
ppt.setSlideOrder(selectesdslide, 1);
下面是重新排列演示文稿中幻灯片的完整程序 -
Given below is the complete program to reorder the slides in a presentation −
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 ,然后从命令提示符编译并执行它,如下所示:
Save the above Java code as ReorderSlide.java, and then compile and execute it from the command prompt as follows −
$javac ReorderSlide.java
$java ReorderSlide
它会编译并执行以生成以下输出。
It will compile and execute to generate the following output.
Reordering of the slides is done
下面是重新排序幻灯片前的演示文稿的快照:
Given below is the snapshot of the presentation before reordering the slides −
重新排序幻灯片后,演示文稿会以如下方式出现。在这里,我们选中包括图像的幻灯片并将其移到顶部。
After reordering the slides, the presentation appears as follows. Here we have selected the slide with image and moved it to the top.
Deleting Slides
您可以使用 removeSlide() 方法来删除幻灯片。按照下列步骤删除幻灯片。
You can delete the slides using the removeSlide() method. Follow the steps given below to delete slides.
使用 XMLSlideShow 类打开现有的演示文稿,如下所示:
Open an existing presentation using the XMLSlideShow class as shown below −
File file = new File("C://POIPPT//Examples//image.pptx");
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
使用 removeSlide() 方法删除所需的幻灯片。此方法接受一个整数参数。将要删除的幻灯片的索引传递给此方法。
Delete the required slide using the removeSlide() method. This method accepts an integer parameter. Pass the index of the slide that is to be deleted to this method.
ppt.removeSlide(1);
以下是从演示文稿中删除幻灯片的程序:
Given below is the program to delete slides from a presentation −
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 ,然后从命令提示符编译并执行它,如下所示:
Save the above Java code as Deleteslide.java, and then compile and execute it from the command prompt as follows −
$javac Deleteslide.java
$java Deleteslide
它将编译和执行以生成以下输出 −
It will compile and execute to generate the following output −
reordering of the slides is done
以下快照显示的是删除幻灯片前的演示文稿:
The snapshot below is of the presentation before deleting the slide −
删除幻灯片后,演示文稿如下所示 −
After deleting the slide, the presentation appears as follows −