Apache Poi Ppt 简明教程

Apache POI PPT - Slide Layouts

在上一章中,你已经了解了如何创建空幻灯片以及如何向其中添加幻灯片。在本章中,您将学习如何获取可用幻灯片的列表,以及如何创建具有不同布局的幻灯片。

In the previous chapter, you have seen how to create empty slides and how to add slides to it. In this chapter, you will learn how to get the list of available slides, and how to create a slide with different layouts.

Available Slide layouts

PowerPoint 演示文稿具有幻灯片布局,您可以选择所需布局来编辑幻灯片。首先,让我们找出所有可用幻灯片布局的列表。

PowerPoint presentations have slide layouts, and you can choose a desired layout to edit a slide. First of all, let us find out the list of all the slide layouts available.

  1. There are different slide masters and in each slide master, there are several slide layouts.

  2. You can get the list of the slide masters using the getSlideMasters() method of the XMLSlideShow class.

  3. You can get the list of the slide layouts from each slide master using the getSlideLayouts() method of the XSLFSlideMaster class.

  4. You can get the name of the slide layout from the layout object using the getType() method of the XSLFSlideLayout class.

Note − 所有这些类都属于 org.poi.xslf.usermodel 包。

Note − All these classes belongs to org.poi.xslf.usermodel package.

下面提供了获取 PPT 中可用幻灯片布局的完整程序 −

Given below is the complete program to get the list of available slide layouts in the PPT −

import java.io.IOException;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster;

public class SlideLayouts {
   public static void main(String args[]) throws IOException {
      //create an empty presentation
      XMLSlideShow ppt = new XMLSlideShow();
      System.out.println("Available slide layouts:");

      //getting the list of all slide masters
      for(XSLFSlideMaster master : ppt.getSlideMasters()) {
         //getting the list of the layouts in each slide master
         for(XSLFSlideLayout layout : master.getSlideLayouts()) {
            //getting the list of available slides
            System.out.println(layout.getType());
         }
      }
   }
}

将上面的 Java 代码另存为 SlideLayouts.java ,然后按如下所示从命令提示符对其进行编译和执行 −

Save the above Java code as SlideLayouts.java , and then compile and execute it from the command prompt as follows −

$javac SlideLayouts.java
$java SlideLayouts

它将编译和执行以生成以下输出 −

It will compile and execute to generate the following output −

Available slide layouts:
TITLE
PIC_TX
VERT_TX
TWO_TX_TWO_OBJ
BLANK
VERT_TITLE_AND_TX
TITLE_AND_CONTENT
TITLE_ONLY
SECTION_HEADER
TWO_OBJ
OBJ_TX

下面展示了一些适用于 MS-Office 360、2013 版本的幻灯片布局示例。

Shown below are some of the sample slide layouts available with MS-Office 360, 2013 edition.

sample slide layouts

Title Layout

让我们使用标题布局在 PPT 中创建一个幻灯片。请按照以下步骤操作 −

Let us create a slide in a PPT using Title layout. Follow the steps given below −

Step 1 − 通过实例化来创建一个空演示文稿 XMLSlideShow 如下所示。

Step 1 − Create an empty presentation by instantiating the XMLSlideShow class as shown below.

XMLSlideShow ppt = new XMLSlideShow();

Step 2 − 使用 getSlideMasters() 方法获取幻灯片母版列表。此后,使用索引选择所需的幻灯片母版,如下所示。

Step 2 − Get the list of slide masters using the getSlideMasters() method. Thereafter, select the desired slide master using the index as shown below.

XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];

这里我们获取在幻灯片母版数组中位于第 0 个位置的默认幻灯片母版。

Here we are getting the default slide master which is in the 0th location of the slide masters array.

Step 3 − 使用 XSLFSlideMaster 类的 getLayout() 方法获取所需的布局。此方法接受一个参数,你必须在其中传递 SlideLayoutclass 的静态变量之一,它代表我们的所需布局。此类中有多个变量,每个变量都代表一个幻灯片布局。

Step 3 − Get the desired layout using the getLayout() method of the XSLFSlideMaster class. This method accepts a parameter where you have to pass one of the static variable of the SlideLayoutclass, which represents our desired layout. There are several variables in this class where each variable represents a slide layout.

下面给出的代码片段演示如何创建一个标题布局 −

The code snippet given below shows how to create a title layout −

XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE);

Step 4 − 通过传递一个幻灯片布局对象作为参数来创建一个新幻灯片。

Step 4 − Create a new slide by passing a slide layout object as parameter.

XSLFSlide slide = ppt.createSlide(titleLayout);

Step 5 − 使用 XSLFSlide 类的 getPlaceholder() 方法选择一个占位符。此方法接受一个整数参数。通过向其传递 0 ,你将获取 XSLFTextShape 对象,使用此对象,你可以访问幻灯片的标题文本区。使用 setText() 方法设置标题,如下所示。

Step 5 − Select a placeholder using the getPlaceholder() method of the XSLFSlide class. This method accepts an integer parameter. By passing 0 to it, you will get the XSLFTextShape object, using which you can access the title text area of the slide. Set the title using the setText() method as shown below.

XSLFTextShape title1 = slide.getPlaceholder(0);
//setting the title init
title1.setText("Tutorials point");

下面给出了在演示文稿中使用标题布局创建幻灯片的完整程序 −

Given below is the complete program to create a slide with Title layout in a presentation −

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

public class TitleLayout {
   public static void main(String args[]) throws IOException {
      //creating presentation
      XMLSlideShow ppt = new XMLSlideShow();

      //getting the slide master object
      XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0);

      //get the desired slide layout
      XSLFSlideLayout titleLayout = slideMaster.getLayout(SlideLayout.TITLE);

      //creating a slide with title layout
      XSLFSlide slide1 = ppt.createSlide(titleLayout);

      //selecting the place holder in it
      XSLFTextShape title1 = slide1.getPlaceholder(0);

      //setting the title init
      title1.setText("Tutorials point");

      //create a file object
      File file = new File("F://Titlelayout.pptx");
      FileOutputStream out = new FileOutputStream(file);

      //save the changes in a PPt document
      ppt.write(out);
      System.out.println("slide cretated successfully");
      out.close();
   }
}

将上面的 Java 代码另存为 TitleLayout.java,然后按如下所示从命令提示符对其进行编译和执行 −

Save the above Java code as TitleLayout.java, and then compile and execute it from the command prompt as follows −

$javac TitleLayout.java
$java TitleLayout

它会编译并执行以生成以下输出。

It will compile and execute to generate the following output.

slide created successfully

新加的标题版式幻灯片的 PPT 文档显示如下所示 -

The PPT document with newly added Title layout slide appears as follows −

TitleLayOut

Title and content Layout

使用标题和内容版式在 PPT 中创建幻灯片。遵循以下给出的步骤。

Let us create a slide in a PPT using Title and content layout. Follow the steps given below.

Step 1 − 通过实例化来创建一个空演示文稿 XMLSlideShow 如下所示。

Step 1 − Create an empty presentation by instantiating the XMLSlideShow class as shown below.

XMLSlideShow ppt = new XMLSlideShow();

Step 2 - 使用 getSlideMasters() 方法获取幻灯片母版列表。使用索引选择所需的幻灯片母版,如下所示。

Step 2 − Get the list of slide masters using the getSlideMasters() method. Select the desired slide master using the index as shown below.

XSLFSlideMaster slideMaster = ppt.getSlideMasters()[0];

这里我们获取在幻灯片母版数组中位于第 0 个位置的默认幻灯片母版。

Here we are getting the default slide master which is in the 0th location of the slide masters array.

Step 3 - 使用 XSLFSlideMaster 类的 getLayout() 方法获取所需的版式。此方法接受一个参数,您必须在其中传递表示所需版式的 SlideLayout 类的 static 变量之一。此类中有几个表示幻灯片版式的变量。

Step 3 − Get the desired layout using the getLayout() method of the XSLFSlideMaster class. This method accepts a parameter where you have to pass one of the static variable of the SlideLayout class which represents our desired layout. There are several variables in this class that represent slide layouts.

以下代码片段显示如何创建标题和内容版式 -

The following code snippet shows how to create title and content layout −

XSLFSlideLayout contentlayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);

Step 4 - 通过传递幻灯片版式对象作为参数来创建新幻灯片。

Step 4 − Create a new slide by passing the slide layout object as parameter.

XSLFSlide slide = ppt.createSlide(SlideLayout.TITLE_AND_CONTENT);

Step 5 - 使用 XSLFSlide 类的 getPlaceholder() 方法选择占位符。此方法接受一个整数参数。通过将 1 传递给它,您将获得 XSLFTextShape 对象,使用该对象您可以访问幻灯片的内容区域。使用 setText() 方法设置标题,如下所示。

Step 5 − Select a placeholder using the getPlaceholder() method of the XSLFSlide class. This method accepts an integer parameter. By passing 1 to it, you will get the XSLFTextShape object, using which you can access the content area of the slide. Set the title using the setText() method as shown below.

XSLFTextShape title1 = slide1.getPlaceholder(1);
//setting the title init
title1.setText("Introduction");

Step 6 - 使用 XSLFTextShape 类的 clearText() 方法清除幻灯片中的现有文本。

Step 6 − Clear the existing text in the slide using the clearText() method of the XSLFTextShape class.

body.clearText();

Step 7 - 使用 addNewTextParagraph() 方法添加新段落。现在使用 addNewTextRun() 方法向段落添加新的文本运行。现在将文本添加到文本运行,使用 setText() 方法,如下所示。

Step 7 − Add new paragraph using the addNewTextParagraph() method. Now add a new text run to the paragraph using the addNewTextRun() method. Now to the text run, add text using the setText() method as shown below.

body.addNewTextParagraph().addNewTextRun().setText("this is  my first slide body");

下面给出了在演示文稿中使用标题布局创建幻灯片的完整程序 −

Given below is the complete program to create a slide with Title layout in a presentation −

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import org.apache.poi.xslf.usermodel.XSLFTextShape;

public class TitleAndBodyLayout {
   public static void main(String args[]) throws IOException {
      //creating presentation
      XMLSlideShow ppt = new XMLSlideShow();

      //getting the slide master object
      XSLFSlideMaster slideMaster = ppt.getSlideMasters().get(0);

      //select a layout from specified list
      XSLFSlideLayout slidelayout = slideMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);

      //creating a slide with title and content layout
      XSLFSlide slide = ppt.createSlide(slidelayout);

      //selection of title place holder
      XSLFTextShape title = slide.getPlaceholder(0);

      //setting the title in it
      title.setText("introduction");

      //selection of body placeholder
      XSLFTextShape body = slide.getPlaceholder(1);

      //clear the existing text in the slide
      body.clearText();

      //adding new paragraph
      body.addNewTextParagraph().addNewTextRun().setText("this is  my first slide body");

      //create a file object
      File file = new File("contentlayout.pptx");
      FileOutputStream out = new FileOutputStream(file);

      //save the changes in a file
      ppt.write(out);
      System.out.println("slide cretated successfully");
      out.close();
   }
}

将上述 Java 代码另存为 TitleLayout.java ,然后从命令提示符编译并执行它,如下所示 -

Save the above Java code as TitleLayout.java, and then compile and execute it from the command prompt as follows −

$javac TitleLayout.java
$java TitleLayout

它将编译和执行以生成以下输出 −

It will compile and execute to generate the following output −

slide created successfully

新加的标题版式幻灯片的 PPT 文档显示如下所示 -

The PPT document with newly added Title layout slide appears as follows −

TitleAndContentLayout

以相同的方式,您也可以创建具有不同版式的幻灯片。

In the same way, you can create slides with different layouts as well.