poi操作PPT使用记录
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.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
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
How to retrieve or change slide size
如何检索或更改幻灯片大小
XMLSlideShow ppt = new XMLSlideShow();
//retrieve page size. Coordinates are expressed in points (72 dpi)
java.awt.Dimension pgsize = ppt.getPageSize();
int pgx = pgsize.width; //slide width in points
int pgy = pgsize.height; //slide height in points
//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
} 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("http://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]
Options:
-scale scale factor (default is 1.0)
-slide 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图形)方法,该方法以递归方式将幻灯片中的所有形状绘制到提供的图形画布中:
slide.draw(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.
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();
上一篇: javascript如何进行调试
下一篇: windows10摄像头不能用怎么办