apache poi powerpoint布局

本文概述

  • Apache POI标题幻灯片示例
  • Apache POI幻灯片内容示例
我们可以设置幻灯片布局,标题和内容。在下面的示例中,我们将创建标题幻灯片。
Apache POI标题幻灯片示例
package poiexample; import java.io.FileOutputStream; import java.io.OutputStream; 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 CreatingPptExample { public static void main(String[] args) { XMLSlideShow ppt = new XMLSlideShow(); try (OutputStream os = new FileOutputStream("srcmini.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); } } }

输出:
Following slide has title which was set by the Java program.

apache poi powerpoint布局

文章图片
除标题外,现在下面的示例将内容设置为创建的幻灯片。
Apache POI幻灯片内容示例
package poiexample; import java.io.FileOutputStream; import java.io.OutputStream; 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 TitleContentExample { public static void main(String[] args) { XMLSlideShow ppt = new XMLSlideShow(); try (OutputStream os = new FileOutputStream("srcmini.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); } } }

【apache poi powerpoint布局】输出:
apache poi powerpoint布局

文章图片

    推荐阅读