POI-PPT官方文档

注意

请注意,XSLF仍然处于早期开发阶段,并且将来会在发行版中发生不兼容的更改。

特征索引

创建新的演示文稿
阅读现有演示文稿
使用预定义的布局创建幻灯片
删除幻灯片
重新订购幻灯片
更改幻灯片大小
阅读形状
添加图片
阅读演示文稿中包含的图像
格式化文本
超链接
将.pptx幻灯片转换为图像
合并多个演示文稿

新演示文稿

以下代码创建一个新的.pptx幻灯片,并向其添加一个空白幻灯片:

//创建一个新的空幻灯片
XMLSlideShow ppt = new XMLSlideShow();

//添加第一张幻灯片
XSLFSlide blankSlide = ppt.createSlide();

阅读现有演示文稿并附加幻灯片

XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));

//附加一个新的幻灯片到最后
XSLFSlide blankSlide = ppt.createSlide();

从预定义的幻灯片布局创建新的幻灯片

XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));

//首先看看什么幻灯片布局可用:
System.out.println("可用幻灯片布局:");
for(XSLFSlideMaster master:ppt.getSlideMasters()){
    for(XSLFSlideLayout layout:master.getSlideLayouts()){
        的System.out.println(layout.getType());
    }
}

//空白幻灯片
XSLFSlide blankSlide = ppt.createSlide();

//可以有多个主器件引用多个布局
//为了演示,我们使用第一个(默认)幻灯片母版
XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);

//标题幻灯片
XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE);
//填写占位符
XSLFSlide slide1 = ppt.createSlide(titleLayout);
XSLFTextShape title1 = slide1.getPlaceholder(0);
title1.setText("First Title");

//标题和内容
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(); //取消设置任何现有文本
body2.addNewTextParagraph().addNewTextRun().setText("First paragraph");
body2.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
body2.addNewTextParagraph().addNewTextRun().setText("Third paragraph");

删除幻灯片

XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));

ppt.removeSlide(0); //要删除的幻灯片的基于0的索引

重新排列幻灯片

XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
列表<XSLFSlide> slides = ppt.getSlides();

XSLFSlide thirdSlide = slides.get(2);
ppt.setSlideOrder(thirdSlide,0); //将第三张幻灯片移动到开头

如何检索或更改幻灯片大小

XMLSlideShow ppt = new XMLSlideShow();
//检索页面大小。坐标表示为点数(72 dpi)
java.awt.Dimension pgsize = ppt.getPageSize();
int pgx = pgsize.width; //滑动宽度的点数
int pgy = pgsize.height; //滑动高度的点数

//设置新页面大小
ppt.setPageSize(new java.awt.Dimension(1024,768));

如何读取特定幻灯片中包含的形状

以下代码演示了如何迭代每个幻灯片的形状。

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
        } 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
        }
    }
}

将图片添加到幻灯片

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);

阅读演示文稿中包含的图片

    XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
    for(XSLFPictureData data : ppt.getAllPictures()){
        byte[] bytes = data.getData();
        String fileName = data.getFileName();
        
    }

基本文本格式

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);

如何创建超链接

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("http://poi.apache.org");

PPTX2PNG是将.pptx幻灯片的每个幻灯片转换为PNG图像的应用程序

  • 用法:PPTX2PNG [选项] <pptx文件>

  • 选项:
    -scale 比例因子(默认值为1.0)
    -slide 1的幻灯片的索引。默认是渲染所有幻灯片。

  • 怎么运行的:
    XSLFSlide对象实现了一个绘制(Graphics2D图形)方法,它将幻灯片中的所有形状递归地绘制到提供的图形画布中:

          slide.draw(graphics);
    

其中图形是实现java.awt.Graphics2D的类。在PPTX2PNG中,图形画布派生自java.awt.image.BufferedImage,即目标是内存中的图像,但在一般情况下,您可以传递任何兼容的java.awt.Graphics2D实现。该 PPTX2SVG 示例演示了如何使用Apache蜡染到幻灯片转换成.PPTX SVG格式。

合并多个演示文稿

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();
posted @ 2017-08-23 22:58  舒山  阅读(3737)  评论(0编辑  收藏  举报