keep rat-check quiet, also started writing xslf docs

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1198722 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2011-11-07 13:22:04 +00:00
parent e021457005
commit afecad3532
13 changed files with 599 additions and 4 deletions

View File

@ -30,7 +30,8 @@
<menu label="HSLF">
<menu-item label="Overview" href="index.html"/>
<menu-item label="Quick Guide" href="quick-guide.html"/>
<menu-item label="Shapes HowTo" href="how-to-shapes.html"/>
<menu-item label="HSLF Cookbok" href="how-to-shapes.html"/>
<menu-item label="XSLF Cookbok" href="xslf-cookbook.html"/>
<menu-item label="PPT File Format" href="ppt-file-format.html"/>
</menu>

View File

@ -63,8 +63,7 @@
Please note that XSLF is still in early development and is a subject to incompatible changes in future.
</p>
<p>
A quick guide is available in
<link href="http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/">XSLF Examples</link>
A quick guide is available in the <link href="./xslf-cookbook.html">XSLF Cookbook</link>
</p>
</section>
</body>

View File

@ -0,0 +1,278 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
====================================================================
-->
<!DOCTYPE document PUBLIC "-//APACHE//DTD Documentation V1.1//EN" "../dtd/document-v11.dtd">
<document>
<header>
<title>XSLF Cookbook</title>
<authors>
<person email="yegor@dinom.ru" name="Yegor Kozlov" id="CO"/>
</authors>
</header>
<body>
<section><title>XSLF Cookbook</title>
<note>
Please note that XSLF is still in early development and is a subject to incompatible changes in a future release.
</note>
<section><title>Index of Features</title>
<ul>
<li><link href="#NewPresentation">Create a new presentation</link></li>
<li><link href="#ReadPresentation">Read an existing presentation</link></li>
<li><link href="#SlideLayout">Create a slide with a predefined layout</link></li>
<li><link href="#DeleteSlide">Delete slide</link></li>
<li><link href="#MoveSlide">Re-order slides</link></li>
<li><link href="#SlideSize">Change slide size</link></li>
<li><link href="#GetShapes">Read shapes</link></li>
<li><link href="#AddImage">Add image</link></li>
<li><link href="#ReadImages">Read images contained in a presentation</link></li>
<li><link href="#Text">Format text</link></li>
<li><link href="#Hyperlinks">Hyperlinks</link></li>
<li><link href="#PPTX2PNG">Convert .pptx slides into images</link></li>
</ul>
</section>
<section><title>Cookbok</title>
<anchor id="NewPresentation"/>
<section><title>New Presentation</title>
<p>
The following code creates a new .pptx slide show and adds a blank slide to it:
</p>
<source>
//create a new empty slide show
XMLSlideShow ppt = new XMLSlideShow();
//add first slide
XSLFSlide blankSlide = ppt.createSlide();
</source>
</section>
<anchor id="ReadPresentation"/>
<section><title>Read an existing presentation and append a slide to it</title>
<source>
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
//append a new slide to the end
XSLFSlide blankSlide = ppt.createSlide();
</source>
</section>
<anchor id="SlideLayout"/>
<section><title>Create a new slide from a predefined slide layout</title>
<source>
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()[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");
</source>
</section>
<anchor id="DeleteSlide"/>
<section><title>Delete slide</title>
<source>
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
ppt.removeSlide(0); // 0-based index of a slide to be removed
</source>
</section>
<anchor id="MoveSlide"/>
<section><title>Re-order slides</title>
<source>
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
XSLFSlide[] slides = ppt.getSlides();
XSLFSlide thirdSlide = slides[2];
ppt.setSlideOrder(thirdSlide, 0); // move the third slide to the beginning
</source>
</section>
<anchor id="SlideSize"/>
<section><title>How to retrieve or change slide size</title>
<source>
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));
</source>
</section>
<anchor id="GetShapes"/>
<section><title>How to read shapes contained in a particular slide</title>
<p>
The following code demonstrates how to iterate over shapes for each slide.
</p>
<source>
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
//get slides
XSLFSlide[] slide = ppt.getSlides();
for (int i = 0; i &lt; slide.length; i++){
XSLFShape[] sh = slide[i].getShapes();
for (int j = 0; j &lt; sh.length; j++){
//name of the shape
String name = sh[j].getShapeName();
//shapes's anchor which defines the position of this shape in the slide
java.awt.geom.Rectangle2D anchor = sh[j].getAnchor();
if (sh[j] instanceof XSLFConnectorShape){
XSLFConnectorShape line = (XSLFConnectorShape)sh[j];
//work with Line
} else if (sh[j] instanceof XSLFTextShape){
XSLFTextShape shape = (XSLFTextShape)sh[j];
//work with a shape that can hold text
} else if (sh[j] instanceof XSLFPictureShape){
XSLFPictureShape shape = (XSLFPictureShape)sh[j];
//work with Picture
}
}
}
</source>
</section>
<anchor id="AddImage"/>
<section><title>Add Image to Slide</title>
<source>
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
byte[] pictureData = IOUtils.toByteArray(new FileInputStream("image.png"));
int idx = ppt.addPicture(pictureData, XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape pic = slide.createPicture(idx);
</source>
</section>
<anchor id="ReadImages"/>
<section><title>Read Images contained within a presentation</title>
<source>
XMLSlideShow ppt = new XMLSlideShow(new FileInputStream("slideshow.pptx"));
for(XSLFPictureData data : ppt.getAllPictures()){
byte[] bytes = data.getData();
String fileName = data.getFileName();
}
</source>
</section>
<anchor id="Text"/>
<section><title>Basic text formatting</title>
<source>
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);
</source>
</section>
<anchor id="Hyperlinks"/>
<section><title>How to read hyperlinks from a slide show</title>
<source>
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");
</source>
</section>
<anchor id="PPTX2PNG"/>
<section><title>PPTX2PNG is an application that converts each slide of a .pptx slideshow into a PNG image</title>
<source>
Usage: PPTX2PNG [options] &lt;pptx file&gt;
Options:
-scale &lt;float&gt; scale factor (default is 1.0)
-slide &lt;integer&gt; 1-based index of a slide to render. Default is to render all slides.
</source>
<p>How it works:</p>
<p>
The XSLFSlide object implements a draw(Graphics2D graphics) method that recursively paints all shapes
in the slide into the supplied graphics canvas:
</p>
<source>
slide.draw(graphics);
</source>
<p>
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
<link href="http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/PPTX2SVG.txt">PPTX2SVG</link>
example demonstrates how to use Apache Batik to convert .pptx slides into SVG format.
</p>
</section>
</section>
</section>
</body>
</document>

View File

@ -0,0 +1,176 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.xslf.usermodel;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.batik.transcoder.wmf.tosvg.WMFPainter;
import org.apache.batik.transcoder.wmf.tosvg.WMFRecordStore;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import javax.imageio.ImageIO;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
/**
* Convert each slide of a .pptx presentation into SVG
*
* @author Yegor Kozlov
*/
public class PPTX2SVG {
static void usage() {
System.out.println("Usage: PPTX2SVG <pptx file>");
}
public static void main(String[] args) throws Exception {
if (args.length == 0) {
usage();
return;
}
String file = args[0];
System.out.println("Processing " + file);
// read the .pptx file
XMLSlideShow ppt = new XMLSlideShow(OPCPackage.open(file));
Dimension pgsize = ppt.getPageSize();
// convert each slide into a .svg file
XSLFSlide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
// Create initial SVG DOM
DOMImplementation domImpl = SVGDOMImplementation.getDOMImplementation();
Document doc = domImpl.createDocument("http://www.w3.org/2000/svg", "svg", null);
//Use Batik SVG Graphics2D driver
SVGGraphics2D graphics = new SVGGraphics2D(doc);
graphics.setRenderingHint(XSLFRenderingHint.IMAGE_RENDERER, new WMFImageRender());
graphics.setSVGCanvasSize(pgsize);
String title = slide[i].getTitle();
System.out.println("Rendering slide " + (i + 1) + (title == null ? "" : ": " + title));
// draw stuff. All the heavy-lifting happens here
slide[i].draw(graphics);
// save the result.
int sep = file.lastIndexOf(".");
String fname = file.substring(0, sep == -1 ? file.length() : sep) + "-" + (i + 1) + ".svg";
OutputStreamWriter out =
new OutputStreamWriter(new FileOutputStream(fname), "UTF-8");
DOMSource domSource = new DOMSource(graphics.getRoot());
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.transform(domSource, streamResult);
out.flush();
out.close();
}
System.out.println("Done");
}
/**
* Image renderer with support for .wmf images
*/
static class WMFImageRender extends XSLFImageRendener {
/**
* Use Apache Batik to render WMF,
* delegate all other types of images to the javax.imageio framework
*/
@Override
public boolean drawImage(Graphics2D graphics, XSLFPictureData data,
Rectangle2D anchor) {
try {
// see what type of image we are
PackagePart part = data.getPackagePart();
String contentType = part.getContentType();
if (contentType.equals("image/x-wmf")) {
WMFRecordStore currentStore = new WMFRecordStore();
currentStore.read(new DataInputStream(part.getInputStream()));
int wmfwidth = currentStore.getWidthPixels();
float conv = (float) anchor.getWidth() / wmfwidth;
// Build a painter for the RecordStore
WMFPainter painter = new WMFPainter(currentStore,
(int) anchor.getX(), (int) anchor.getY(), conv);
painter.paint(graphics);
} else {
BufferedImage img = ImageIO.read(data.getPackagePart().getInputStream());
graphics.drawImage(img,
(int) anchor.getX(), (int) anchor.getY(),
(int) anchor.getWidth(), (int) anchor.getHeight(), null);
}
} catch (Exception e) {
return false;
}
return true;
}
/**
* Convert data form the supplied package part into a BufferedImage.
* This method is used to create texture paint.
*/
@Override
public BufferedImage readImage(PackagePart packagePart) throws IOException {
String contentType = packagePart.getContentType();
if (contentType.equals("image/x-wmf")) {
try {
WMFRecordStore currentStore = new WMFRecordStore();
currentStore.read(new DataInputStream(packagePart.getInputStream()));
int wmfwidth = currentStore.getWidthPixels();
int wmfheight = currentStore.getHeightPixels();
BufferedImage img = new BufferedImage(wmfwidth, wmfheight, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
// Build a painter for the RecordStore
WMFPainter painter = new WMFPainter(currentStore, 0, 0, 1.0f);
painter.paint(graphics);
return img;
} catch (IOException e) {
return null;
}
} else {
return ImageIO.read(packagePart.getInputStream());
}
}
}
}

View File

@ -1,3 +1,21 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.hpsf;
import org.apache.poi.util.Internal;

View File

@ -1,3 +1,21 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.hpsf;
import org.apache.poi.util.Internal;

View File

@ -510,6 +510,22 @@ public class XSLFTextParagraph implements Iterable<XSLFTextRun>{
return fetcher.getValue() == null ? false : fetcher.getValue();
}
/**
*
* @param isBullet whether text in this paragraph has bullets
*/
public void setBullet(boolean flag) {
if(isBullet() == flag) return;
CTTextParagraphProperties pr = _p.isSetPPr() ? _p.getPPr() : _p.addNewPPr();
if(!flag) {
pr.addNewBuNone();
} else {
pr.addNewBuFont().setTypeface("Arial");
pr.addNewBuChar().setChar("\u2022");
}
}
@Override
public String toString(){
return "[" + getClass() + "]" + getText();

View File

@ -1,3 +1,21 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.xslf.geom;
import junit.framework.TestCase;

View File

@ -1,3 +1,21 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.xslf.geom;
import junit.framework.TestCase;

View File

@ -1,3 +1,21 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.hwpf.model;
import java.io.IOException;

View File

@ -1,4 +1,21 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.hwpf.model.types;

View File

@ -1,3 +1,21 @@
/*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.apache.poi.hwpf.sprm;
import org.apache.poi.hwpf.usermodel.TableProperties;