xlsf tutorial and cookbook updates

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1212978 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2011-12-11 08:43:09 +00:00
parent 81cff4c751
commit 95d0ab557b
11 changed files with 569 additions and 87 deletions

View File

@ -23,11 +23,16 @@
<header> <header>
<title>XSLF Cookbook</title> <title>XSLF Cookbook</title>
<authors> <authors>
<person email="yegor@dinom.ru" name="Yegor Kozlov" id="CO"/> <person email="yegor@apache.org" name="Yegor Kozlov" id="YK"/>
</authors> </authors>
</header> </header>
<body> <body>
<section><title>XSLF Cookbook</title> <section><title>XSLF Cookbook</title>
<p>
This page offers a short introduction into the XSLF API. More examples can be found in the
<link href="http://svn.apache.org/repos/asf/poi/trunk/src/examples/src/org/apache/poi/xslf/usermodel/"> XSLF Examples</link>
in the POI SVN repository.
</p>
<note> <note>
Please note that XSLF is still in early development and is a subject to incompatible changes in a future release. Please note that XSLF is still in early development and is a subject to incompatible changes in a future release.
</note> </note>

View File

@ -0,0 +1,94 @@
/*
* ====================================================================
* 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.poi.openxml4j.opc.PackagePart;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
/**
* Demonstrates how you can extract data from a .pptx file
*
* @author Yegor Kozlov
*/
public final class DataExtraction {
public static void main(String args[]) throws Exception {
if (args.length == 0) {
System.out.println("Input file is required");
return;
}
FileInputStream is = new FileInputStream(args[0]);
XMLSlideShow ppt = new XMLSlideShow(is);
is.close();
// Get the document's embedded files.
List<PackagePart> embeds = ppt.getAllEmbedds();
for (PackagePart p : embeds) {
String type = p.getContentType();
String name = p.getPartName().getName(); //typically file name
InputStream pIs = p.getInputStream();
// make sense of the part data
pIs.close();
}
// Get the document's embedded files.
List<XSLFPictureData> images = ppt.getAllPictures();
for (XSLFPictureData data : images) {
PackagePart p = data.getPackagePart();
String type = p.getContentType();
String name = data.getFileName();
InputStream pIs = p.getInputStream();
// make sense of the image data
pIs.close();
}
Dimension pageSize = ppt.getPageSize(); // size of the canvas in points
for(XSLFSlide slide : ppt.getSlides()) {
for(XSLFShape shape : slide){
Rectangle2D anchor = shape.getAnchor(); // position on the canvas
if(shape instanceof XSLFTextShape) {
XSLFTextShape txShape = (XSLFTextShape)shape;
System.out.println(txShape.getText());
} else if (shape instanceof XSLFPictureShape){
XSLFPictureShape pShape = (XSLFPictureShape)shape;
XSLFPictureData pData = pShape.getPictureData();
System.out.println(pData.getFileName());
} else {
System.out.println("Process me: " + shape.getClass());
}
}
}
}
}

View File

@ -0,0 +1,50 @@
/*
* ====================================================================
* 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 java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* Merge multiple pptx presentations together
*
* @author Yegor Kozlov
*/
public final class MergePresentations {
public static void main(String args[]) throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
for(String arg : args){
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();
}
}

View File

@ -1,86 +0,0 @@
/* ====================================================================
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 java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import java.io.FileOutputStream;
/**
* Simple demo that creates a pptx slide show using the XSLF API
*
* @author Yegor Kozlov
*/
public class SlidesAndShapes {
public static void main(String[] args) throws Exception {
XMLSlideShow ppt = new XMLSlideShow();
ppt.setPageSize(new Dimension(792, 612));
XSLFSlide slide1 = ppt.createSlide();
XSLFTextBox textBox = slide1.createTextBox();
XSLFTextRun r1 = textBox.addNewTextParagraph().addNewTextRun();
r1.setBold(true);
r1.setItalic(true);
r1.setFontColor(Color.yellow);
r1.setFontFamily("Arial");
r1.setFontSize(24);
r1.setText("Apache");
XSLFTextRun r2 = textBox.addNewTextParagraph().addNewTextRun();
r2.setStrikethrough(true);
r2.setUnderline(true);
r2.setText("POI\u2122");
XSLFTextRun r3 = textBox.addNewTextParagraph().addNewTextRun();
r3.setFontFamily("Wingdings");
r3.setText(" Version 3.8");
textBox.setAnchor(new Rectangle(50, 50, 200, 100));
textBox.setLineColor(Color.black);
textBox.setFillColor(Color.orange);
XSLFAutoShape shape2 = slide1.createAutoShape();
shape2.setAnchor(new Rectangle(100, 100, 200, 200));
XSLFFreeformShape shape3 = slide1.createFreeform();
Rectangle rect = new Rectangle(150, 150, 300, 300);
GeneralPath path = new GeneralPath(rect);
path.append(new Ellipse2D.Double(200, 200, 100, 50), false);
shape3.setPath(path);
shape3.setAnchor(path.getBounds2D());
shape3.setLineColor(Color.black);
shape3.setFillColor(Color.lightGray);
XSLFSlide slide2 = ppt.createSlide();
XSLFGroupShape group = slide2.createGroup();
group.setAnchor(new Rectangle(0, 0, 792, 612));
group.setInteriorAnchor(new Rectangle(-10, -10, 20, 20));
XSLFAutoShape shape4 = group.createAutoShape();
shape4.setAnchor(new Rectangle(0, 0, 5, 5));
shape4.setLineWidth(5);
shape4.setLineColor(Color.black);
FileOutputStream out = new FileOutputStream("xslf-demo.pptx");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,72 @@
/*
* ====================================================================
* 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 java.io.FileOutputStream;
import java.io.IOException;
/**
* Demonstrates how to create slides with predefined layout
* and fill the placeholder shapes
*
* @author Yegor Kozlov
*/
public class Tutorial1 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
// XSLFSlide#createSlide() with no arguments creates a blank slide
XSLFSlide blankSlide = ppt.createSlide();
XSLFSlideMaster master = ppt.getSlideMasters()[0];
XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE);
XSLFSlide slide1 = ppt.createSlide(layout1) ;
XSLFTextShape[] ph1 = slide1.getPlaceholders();
XSLFTextShape titlePlaceholder1 = ph1[0];
titlePlaceholder1.setText("This is a title");
XSLFTextShape subtitlePlaceholder1 = ph1[1];
subtitlePlaceholder1.setText("this is a subtitle");
XSLFSlideLayout layout2 = master.getLayout(SlideLayout.TITLE_AND_CONTENT);
XSLFSlide slide2 = ppt.createSlide(layout2) ;
XSLFTextShape[] ph2 = slide2.getPlaceholders();
XSLFTextShape titlePlaceholder2 = ph2[0];
titlePlaceholder2.setText("This is a title");
XSLFTextShape bodyPlaceholder = ph2[1];
// we are going to add text by paragraphs. Clear the default placehoder text before that
bodyPlaceholder.clearText();
XSLFTextParagraph p1 = bodyPlaceholder.addNewTextParagraph();
p1.setLevel(0);
p1.addNewTextRun().setText("Level1 text");
XSLFTextParagraph p2 = bodyPlaceholder.addNewTextParagraph();
p2.setLevel(1);
p2.addNewTextRun().setText("Level2 text");
XSLFTextParagraph p3 = bodyPlaceholder.addNewTextParagraph();
p3.setLevel(3);
p3.addNewTextRun().setText("Level3 text");
FileOutputStream out = new FileOutputStream("slides.pptx");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,83 @@
/*
* ====================================================================
* 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 java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Basic paragraph and text formatting
*
* @author Yegor Kozlov
*/
public class Tutorial2 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide1 = ppt.createSlide();
XSLFTextBox shape1 = slide1.createTextBox();
// initial height of the text box is 100 pt but
Rectangle anchor = new Rectangle(10, 100, 300, 100);
shape1.setAnchor(anchor);
XSLFTextParagraph p1 = shape1.addNewTextParagraph();
XSLFTextRun r1 = p1.addNewTextRun();
r1.setText("Paragraph Formatting");
r1.setFontSize(24);
r1.setFontColor(new Color(85, 142, 213));
XSLFTextParagraph p2 = shape1.addNewTextParagraph();
// If spaceBefore >= 0, then space is a percentage of normal line height.
// If spaceBefore < 0, the absolute value of linespacing is the spacing in points
p2.setSpaceBefore(-20); // 20 pt from the previous paragraph
p2.setSpaceAfter(300); // 3 lines after the paragraph
XSLFTextRun r2 = p2.addNewTextRun();
r2.setText("Paragraph properties apply to all text residing within the corresponding paragraph.");
r2.setFontSize(16);
XSLFTextParagraph p3 = shape1.addNewTextParagraph();
XSLFTextRun r3 = p3.addNewTextRun();
r3.setText("Run Formatting");
r3.setFontSize(24);
r3.setFontColor(new Color(85, 142, 213));
XSLFTextParagraph p4 = shape1.addNewTextParagraph();
p4.setSpaceBefore(-20); // 20 pt from the previous paragraph
p4.setSpaceAfter(300); // 3 lines after the paragraph
XSLFTextRun r4 = p4.addNewTextRun();
r4.setFontSize(16);
r4.setText(
"Run level formatting is the most granular property level and allows " +
"for the specifying of all low level text properties. The text run is " +
"what all paragraphs are derived from and thus specifying various " +
"properties per run will allow for a diversely formatted text paragraph.");
// resize the shape to fit text
shape1.resizeToFitText();
FileOutputStream out = new FileOutputStream("text.pptx");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,47 @@
/*
* ====================================================================
* 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 java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* How to set slide title
*
* @author Yegor Kozlov
*/
public class Tutorial3 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
XSLFTextShape titleShape = slide.createTextBox();
titleShape.setPlaceholder(Placeholder.TITLE);
titleShape.setText("This is a slide title");
titleShape.setAnchor(new Rectangle(50, 50, 400, 100));
FileOutputStream out = new FileOutputStream("title.pptx");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,89 @@
/*
* ====================================================================
* 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 java.awt.*;
import java.awt.geom.Rectangle2D;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* PPTX Tables
*
* @author Yegor Kozlov
*/
public class Tutorial4 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
// XSLFSlide#createSlide() with no arguments creates a blank slide
XSLFSlide slide = ppt.createSlide();
XSLFTable tbl = slide.createTable();
tbl.setAnchor(new Rectangle2D.Double(50, 50, 450, 300));
int numColumns = 3;
int numRows = 5;
XSLFTableRow headerRow = tbl.addRow();
headerRow.setHeight(50);
// header
for(int i = 0; i < numColumns; i++) {
XSLFTableCell th = headerRow.addCell();
XSLFTextParagraph p = th.addNewTextParagraph();
p.setTextAlign(TextAlign.CENTER);
XSLFTextRun r = p.addNewTextRun();
r.setText("Header " + (i+1));
r.setBold(true);
r.setFontColor(Color.white);
th.setFillColor(new Color(79, 129, 189));
th.setBorderBottom(2);
th.setBorderBottomColor(Color.white);
tbl.setColumnWidth(i, 150); // all columns are equally sized
}
// rows
for(int rownum = 0; rownum < numRows; rownum ++){
XSLFTableRow tr = tbl.addRow();
tr.setHeight(50);
// header
for(int i = 0; i < numColumns; i++) {
XSLFTableCell cell = tr.addCell();
XSLFTextParagraph p = cell.addNewTextParagraph();
XSLFTextRun r = p.addNewTextRun();
r.setText("Cell " + (i+1));
if(rownum % 2 == 0)
cell.setFillColor(new Color(208, 216, 232));
else
cell.setFillColor(new Color(233, 247, 244));
}
}
FileOutputStream out = new FileOutputStream("table.pptx");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,50 @@
/*
* ====================================================================
* 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.poi.util.IOUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Images
*
* @author Yegor Kozlov
*/
public class Tutorial5 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide = ppt.createSlide();
File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg");
byte[] data = IOUtils.toByteArray(new FileInputStream(img));
int pictureIndex = ppt.addPicture(data, XSLFPictureData.PICTURE_TYPE_PNG);
XSLFPictureShape shape = slide.createPicture(pictureIndex);
FileOutputStream out = new FileOutputStream("images.pptx");
ppt.write(out);
out.close();
}
}

View File

@ -0,0 +1,59 @@
/*
* ====================================================================
* 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 java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Hyperlinks
*
* @author Yegor Kozlov
*/
public class Tutorial6 {
public static void main(String[] args) throws IOException{
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlide slide1 = ppt.createSlide();
XSLFSlide slide2 = ppt.createSlide();
XSLFTextBox shape1 = slide1.createTextBox();
shape1.setAnchor(new Rectangle(50, 50, 200, 50));
XSLFTextRun r1 = shape1.addNewTextParagraph().addNewTextRun();
XSLFHyperlink link1 = r1.createHyperlink();
r1.setText("http://poi.apache.org"); // visible text
link1.setAddress("http://poi.apache.org"); // link address
XSLFTextBox shape2 = slide1.createTextBox();
shape2.setAnchor(new Rectangle(300, 50, 200, 50));
XSLFTextRun r2 = shape2.addNewTextParagraph().addNewTextRun();
XSLFHyperlink link2 = r2.createHyperlink();
r2.setText("Go to the second slide"); // visible text
link2.setAddress(slide2); // link address
FileOutputStream out = new FileOutputStream("hyperlinks.pptx");
ppt.write(out);
out.close();
}
}

View File

@ -19,6 +19,7 @@
package org.apache.poi.xslf.usermodel; package org.apache.poi.xslf.usermodel;
import org.apache.poi.POIXMLException;
import org.apache.poi.util.Beta; import org.apache.poi.util.Beta;
import org.apache.poi.util.Units; import org.apache.poi.util.Units;
import org.apache.poi.xslf.model.PropertyFetcher; import org.apache.poi.xslf.model.PropertyFetcher;
@ -454,6 +455,24 @@ public abstract class XSLFTextShape extends XSLFSimpleShape implements Iterable<
return drawParagraphs(graphics, 0, 0); return drawParagraphs(graphics, 0, 0);
} }
/**
* Adjust the size of the shape so it encompasses the text inside it.
*
* @return a <code>Rectangle2D</code> that is the bounds of this <code>TextShape</code>.
*/
public Rectangle2D resizeToFitText(){
Rectangle2D anchor = getAnchor();
if(anchor.getWidth() == 0.) throw new POIXMLException(
"Anchor of the shape was not set.");
double height = getTextHeight();
height += 1; // add a pixel to compensate rounding errors
anchor.setRect(anchor.getX(), anchor.getY(), anchor.getWidth(), height);
setAnchor(anchor);
return anchor;
}
/** /**
* break the contained text into lines * break the contained text into lines
*/ */