📅  最后修改于: 2020-11-18 08:51:13             🧑  作者: Mango
完成本章后,您将可以在幻灯片上删除,重新排序以及执行读取和写入操作。
我们可以使用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代码另存为ChangeingSlide.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()方法获取幻灯片,如下所示-
XSLFSlide[] slides = ppt.getSlides();
从幻灯片数组中选择一张幻灯片,然后使用setSlideOrder()方法更改顺序,如下所示-
//selecting the fourth slide
XSLFSlide selectesdslide = slides[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 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
XSLFSlide[] slides = ppt.getSlides();
//selecting the fourth slide
XSLFSlide selectesdslide = slides[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
下面的快照是删除幻灯片之前的演示文稿的快照-
删除幻灯片后,演示文稿如下所示: