This page offers a short introduction into the XSLF API. More examples can be found in the XSLF Examples in the POI SVN repository.(本页简要介绍了 XSLF API。更多示例可以在 POI SVN 存储库中的 XSLF 示例中找到。)
Note
(注意)
Please note that XSLF is still in early development and is a subject to incompatible changes in a future release.
(请注意,XSLF 仍处于早期开发阶段,在未来版本中可能会发生不兼容的更改。)
Index of Features(特征索引)
Cookbook(食谱)
New Presentation(新的演示文稿)
The following code creates a new .pptx slide show and adds a blank slide to it:(以下代码创建一个新的 .pptx 幻灯片放映并向其中添加一张空白幻灯片:)
//create a new empty slide show
//创建一个新的空幻灯片
XMLSlideShow ppt = new XMLSlideShow();
//add first slide
//添加第一张幻灯片
XSLFSlide blankSlide = ppt.createSlide();
Read an existing presentation and append a slide to it(阅读现有演示文稿并将幻灯片附加到其中)
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
//append a new slide to the end
//在最后添加一张新幻灯片
XSLFSlide blankSlide = ppt.createSlide();
Create a new slide from a predefined slide layout(从预定义的幻灯片布局创建新幻灯片)
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
// first see what slide layouts are available :
// 首先看看有哪些幻灯片布局可用:
System.out.println("Available slide layouts:");
for(XSLFSlideMaster master : ppt.getSlideMasters()){
for(XSLFSlideLayout layout : master.getSlideLayouts()){
System.out.println(layout.getType());
}
}
// blank slide
//空白幻灯片
XSLFSlide blankSlide = ppt.createSlide();
// there can be multiple masters each referencing a number of layouts
// for demonstration purposes we use the first (default) slide master
// 可以有多个母版,每个母版引用多个布局 // 出于演示目的,我们使用第一个(默认)幻灯片母版
XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
// title slide
//标题幻灯片
XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
// fill the placeholders
// 填充占位符
XSLFSlide slide1 = ppt.createSlide(titleLayout);
XSLFTextShape title1 = slide1.getPlaceholder(0);
title1.setText("First Title");
// title and content
// 标题和内容
XSLFSlideLayout titleBodyLayout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
XSLFSlide slide2 = ppt.createSlide(titleBodyLayout);
XSLFTextShape title2 = slide2.getPlaceholder(0);
title2.setText("Second Title");
XSLFTextShape body2 = slide2.getPlaceholder(1);
body2.clearText(); // unset any existing text(body2.clearText(); // 取消设置任何现有文本)
body2.addNewTextParagraph().addNewTextRun().setText("First paragraph");
body2.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
body2.addNewTextParagraph().addNewTextRun().setText("Third paragraph");
Delete slide(删除幻灯片)
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
ppt.removeSlide(0); // 0-based index of a slide to be removed(ppt.removeSlide(0); // 要删除的幻灯片的从 0 开始的索引)
Re-order slides(重新排序幻灯片)
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
List<XSLFSlide> slides = ppt.getSlides();
XSLFSlide thirdSlide = slides.get(2);
ppt.setSlideOrder(thirdSlide, 0); // move the third slide to the beginning(ppt.setSlideOrder(thirdSlide, 0); // 将第三张幻灯片移到开头)
How to retrieve or change slide size(如何检索或更改幻灯片大小)
XMLSlideShow ppt = new XMLSlideShow();
//retrieve page size. Coordinates are expressed in points (72 dpi)
//获取页面大小。坐标以点 (72 dpi) 表示
java.awt.Dimension pgsize = ppt.getPageSize();
int pgx = pgsize.width; //slide width in points(int pgx = pgsize.width; //以点为单位的滑动宽度)
int pgy = pgsize.height; //slide height in points(int pgy = pgsize.height; //以点为单位的滑动高度)
//set new page size
//设置新的页面大小
ppt.setPageSize(new java.awt.Dimension(1024, 768));
How to read shapes contained in a particular slide(如何读取特定幻灯片中包含的形状)
The following code demonstrates how to iterate over shapes for each slide.(下面的代码演示了如何迭代每张幻灯片的形状。)
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
// get slides
// 获取幻灯片
for (XSLFSlide slide : ppt.getSlides()) {
for (XSLFShape sh : slide.getShapes()) {
// name of the shape
// 形状名称
String name = sh.getShapeName();
// shapes's anchor which defines the position of this shape in the slide
// 形状的锚点,它定义了这个形状在幻灯片中的位置
if (sh instanceof PlaceableShape) {
java.awt.geom.Rectangle2D anchor = ((PlaceableShape)sh).getAnchor();
}
if (sh instanceof XSLFConnectorShape) {
XSLFConnectorShape line = (XSLFConnectorShape) sh;
// work with Line
// 使用 Line
} else if (sh instanceof XSLFTextShape) {
XSLFTextShape shape = (XSLFTextShape) sh;
// work with a shape that can hold text
// 使用可以容纳文本的形状
} else if (sh instanceof XSLFPictureShape) {
XSLFPictureShape shape = (XSLFPictureShape) sh;
// work with Picture
// 使用图片
}
}
}
Add Image to Slide(将图像添加到幻灯片)
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
byte[] pictureData = IOUtils.toByteArray(new FileInputStream("image.png"));
XSLFPictureData pd = ppt.addPicture(pictureData, PictureData.PictureType.PNG);
XSLFPictureShape pic = slide.createPicture(pd);
Read Images contained within a presentation(阅读演示文稿中包含的图像)
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
for(XSLFPictureData data : ppt.getAllPictures()){
byte[] bytes = data.getData();
String fileName = data.getFileName();
}
Basic text formatting(基本文本格式)
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFTextBox shape = slide.createTextBox();
XSLFTextParagraph p = shape.addNewTextParagraph();
XSLFTextRun r1 = p.addNewTextRun();
r1.setText("The");
r1.setFontColor(Color.blue);
r1.setFontSize(24.);
XSLFTextRun r2 = p.addNewTextRun();
r2.setText(" quick");
r2.setFontColor(Color.red);
r2.setBold(true);
XSLFTextRun r3 = p.addNewTextRun();
r3.setText(" brown");
r3.setFontSize(12.);
r3.setItalic(true);
r3.setStrikethrough(true);
XSLFTextRun r4 = p.addNewTextRun();
r4.setText(" fox");
r4.setUnderline(true);
How to create a hyperlink(如何创建超链接)
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
// assign a hyperlink to a text run
// 为文本运行分配一个超链接
XSLFTextBox shape = slide.createTextBox();
XSLFTextRun r = shape.addNewTextParagraph().addNewTextRun();
r.setText("Apache POI");
XSLFHyperlink link = r.createHyperlink();
link.setAddress("https://poi.apache.org");(link.setAddress ("https://poi.apache.org");)
PPTX2PNG is an application that converts each slide of a .pptx slideshow into a PNG image(PPTX2PNG 是一个将 .pptx 幻灯片的每张幻灯片转换为 PNG 图像的应用程序)
Usage: PPTX2PNG [options] <pptx file>
Options:
-scale <float> scale factor (default is 1.0)
-slide <integer> 1-based index of a slide to render. Default is to render all slides.
How it works:(如何运行:)
The XSLFSlide object implements a draw(Graphics2D graphics) method that recursively paints all shapes in the slide into the supplied graphics canvas:(XSLFSlide 对象实现了一个 draw(Graphics2D graphics) 方法,该方法递归地将幻灯片中的所有形状绘制到提供的图形画布中:)
where graphics is a class implementing java.awt.Graphics2D. In PPTX2PNG the graphic canvas is derived from java.awt.image.BufferedImage, i.e. the destination is an image in memory, but in general case you can pass any compliant implementation of java.awt.Graphics2D. The PPTX2SVG example demonstrates how to use Apache Batik to convert .pptx slides into SVG format.(其中 graphics 是一个实现 java.awt.Graphics2D 的类。在 PPTX2PNG 中,图形画布派生自 java.awt.image.BufferedImage,即目标是内存中的图像,但一般情况下,您可以传递 java.awt.Graphics2D 的任何兼容实现。 PPTX2SVG 示例演示了如何使用 Apache Batik 将 .pptx 幻灯片转换为 SVG 格式。)
Merge multiple presentations together(将多个演示文稿合并在一起)
XMLSlideShow ppt = new XMLSlideShow();
String[] inputs = {"presentations1.pptx", "presentation2.pptx"};
for(String arg : inputs){
FileInputStream is = new FileInputStream(arg);
XMLSlideShow src = new XMLSlideShow(is);
is.close();
for(XSLFSlide srcSlide : src.getSlides()){
ppt.createSlide().importContent(srcSlide);
}
}
FileOutputStream out = new FileOutputStream("merged.pptx");
ppt.write(out);
out.close();