Apache POI PPT 设置幻灯片布局

我们可以设置幻灯片布局,它的标题和内容也是如此。在以下示例中,我们正在创建标题幻灯片。

Apache POI 设置幻灯片标题示例

package com.yiidian;

import org.apache.poi.xslf.usermodel.*;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class CreatingPptExample {
    public static void main(String[] args) {  
        XMLSlideShow ppt = new XMLSlideShow();
        try (OutputStream os = new FileOutputStream("yiidian.pptx")) {
            XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
            XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
            XSLFSlide slide = ppt.createSlide(titleLayout);
            XSLFTextShape title = slide.getPlaceholder(0);  
            title.setText("Enter Title Here");  
            ppt.write(os);  
        }catch(Exception e) {  
            System.out.println(e);  
        }  
    }  
}  

输出结果为:

除了设置标题外,现在下面的示例将为创建的幻灯片设置内容。

Apache POI 设置幻灯片内容示例

package com.yiidian;

import org.apache.poi.xslf.usermodel.*;

import java.io.FileOutputStream;
import java.io.OutputStream;

public class TitleContentExample {
    public static void main(String[] args) {  
        XMLSlideShow ppt = new XMLSlideShow();
        try (OutputStream os = new FileOutputStream("yiidian.pptx")) {
            XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
            XSLFSlideLayout tc = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
            XSLFSlide slide = ppt.createSlide(tc);
            XSLFTextShape title = slide.getPlaceholder(0);  
            title.setText("Title here");  
            XSLFTextShape body = slide.getPlaceholder(1);  
            body.clearText();  
            body.addNewTextParagraph().addNewTextRun().setText("This is a new slide created using Java program.");  
            ppt.write(os);  
        }catch(Exception e) {  
            System.out.println(e);  
        }  
    }  
}  

输出结果为:

热门文章

优秀文章