<header>
<title>XSLF Cookbook</title>
<authors>
- <person email="yegor@dinom.ru" name="Yegor Kozlov" id="CO"/>
+ <person email="yegor@apache.org" name="Yegor Kozlov" id="YK"/>
</authors>
</header>
<body>
<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>
Please note that XSLF is still in early development and is a subject to incompatible changes in a future release.
</note>
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import org.apache.poi.openxml4j.opc.PackagePart;\r
+\r
+import java.awt.*;\r
+import java.awt.geom.Rectangle2D;\r
+import java.io.FileInputStream;\r
+import java.io.InputStream;\r
+import java.util.List;\r
+\r
+/**\r
+ * Demonstrates how you can extract data from a .pptx file\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public final class DataExtraction {\r
+\r
+ public static void main(String args[]) throws Exception {\r
+\r
+ if (args.length == 0) {\r
+ System.out.println("Input file is required");\r
+ return;\r
+ }\r
+\r
+ FileInputStream is = new FileInputStream(args[0]);\r
+ XMLSlideShow ppt = new XMLSlideShow(is);\r
+ is.close();\r
+\r
+ // Get the document's embedded files.\r
+ List<PackagePart> embeds = ppt.getAllEmbedds();\r
+ for (PackagePart p : embeds) {\r
+ String type = p.getContentType();\r
+ String name = p.getPartName().getName(); //typically file name\r
+ \r
+ InputStream pIs = p.getInputStream();\r
+ // make sense of the part data\r
+ pIs.close();\r
+ \r
+ }\r
+\r
+ // Get the document's embedded files.\r
+ List<XSLFPictureData> images = ppt.getAllPictures();\r
+ for (XSLFPictureData data : images) {\r
+ PackagePart p = data.getPackagePart();\r
+\r
+ String type = p.getContentType();\r
+ String name = data.getFileName();\r
+\r
+ InputStream pIs = p.getInputStream();\r
+ // make sense of the image data\r
+ pIs.close();\r
+\r
+\r
+\r
+ }\r
+\r
+ Dimension pageSize = ppt.getPageSize(); // size of the canvas in points\r
+ for(XSLFSlide slide : ppt.getSlides()) {\r
+ for(XSLFShape shape : slide){\r
+ Rectangle2D anchor = shape.getAnchor(); // position on the canvas\r
+ if(shape instanceof XSLFTextShape) {\r
+ XSLFTextShape txShape = (XSLFTextShape)shape;\r
+ System.out.println(txShape.getText());\r
+ } else if (shape instanceof XSLFPictureShape){\r
+ XSLFPictureShape pShape = (XSLFPictureShape)shape;\r
+ XSLFPictureData pData = pShape.getPictureData();\r
+ System.out.println(pData.getFileName());\r
+ } else {\r
+ System.out.println("Process me: " + shape.getClass());\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+}\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+\r
+/**\r
+ * Merge multiple pptx presentations together\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public final class MergePresentations {\r
+\r
+ public static void main(String args[]) throws Exception {\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+\r
+ for(String arg : args){\r
+ FileInputStream is = new FileInputStream(arg);\r
+ XMLSlideShow src = new XMLSlideShow(is);\r
+ is.close();\r
+\r
+ for(XSLFSlide srcSlide : src.getSlides()){\r
+ ppt.createSlide().importContent(srcSlide);\r
+ }\r
+ }\r
+\r
+ FileOutputStream out = new FileOutputStream("merged.pptx");\r
+ ppt.write(out);\r
+ out.close();\r
+ }\r
+\r
+}\r
+++ /dev/null
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-package org.apache.poi.xslf.usermodel;\r
-\r
-import java.awt.*;\r
-import java.awt.geom.Ellipse2D;\r
-import java.awt.geom.GeneralPath;\r
-import java.io.FileOutputStream;\r
-\r
-/**\r
- * Simple demo that creates a pptx slide show using the XSLF API\r
- *\r
- * @author Yegor Kozlov\r
- */\r
-public class SlidesAndShapes {\r
-\r
- public static void main(String[] args) throws Exception {\r
- XMLSlideShow ppt = new XMLSlideShow();\r
- ppt.setPageSize(new Dimension(792, 612));\r
-\r
- XSLFSlide slide1 = ppt.createSlide();\r
- XSLFTextBox textBox = slide1.createTextBox();\r
- XSLFTextRun r1 = textBox.addNewTextParagraph().addNewTextRun();\r
- r1.setBold(true);\r
- r1.setItalic(true);\r
- r1.setFontColor(Color.yellow);\r
- r1.setFontFamily("Arial");\r
- r1.setFontSize(24);\r
- r1.setText("Apache");\r
- XSLFTextRun r2 = textBox.addNewTextParagraph().addNewTextRun();\r
- r2.setStrikethrough(true);\r
- r2.setUnderline(true);\r
- r2.setText("POI\u2122");\r
- XSLFTextRun r3 = textBox.addNewTextParagraph().addNewTextRun();\r
- r3.setFontFamily("Wingdings");\r
- r3.setText(" Version 3.8");\r
-\r
- textBox.setAnchor(new Rectangle(50, 50, 200, 100));\r
- textBox.setLineColor(Color.black);\r
- textBox.setFillColor(Color.orange);\r
-\r
- XSLFAutoShape shape2 = slide1.createAutoShape();\r
-\r
- shape2.setAnchor(new Rectangle(100, 100, 200, 200));\r
-\r
- XSLFFreeformShape shape3 = slide1.createFreeform();\r
- Rectangle rect = new Rectangle(150, 150, 300, 300);\r
- GeneralPath path = new GeneralPath(rect);\r
- path.append(new Ellipse2D.Double(200, 200, 100, 50), false);\r
- shape3.setPath(path);\r
- shape3.setAnchor(path.getBounds2D());\r
- shape3.setLineColor(Color.black);\r
- shape3.setFillColor(Color.lightGray);\r
-\r
- XSLFSlide slide2 = ppt.createSlide();\r
- XSLFGroupShape group = slide2.createGroup();\r
-\r
- group.setAnchor(new Rectangle(0, 0, 792, 612));\r
- group.setInteriorAnchor(new Rectangle(-10, -10, 20, 20));\r
-\r
- XSLFAutoShape shape4 = group.createAutoShape();\r
- shape4.setAnchor(new Rectangle(0, 0, 5, 5));\r
- shape4.setLineWidth(5);\r
- shape4.setLineColor(Color.black);\r
-\r
-\r
- FileOutputStream out = new FileOutputStream("xslf-demo.pptx");\r
- ppt.write(out);\r
- out.close();\r
- }\r
-\r
-}\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Demonstrates how to create slides with predefined layout\r
+ * and fill the placeholder shapes\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Tutorial1 {\r
+\r
+ public static void main(String[] args) throws IOException{\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+\r
+ // XSLFSlide#createSlide() with no arguments creates a blank slide\r
+ XSLFSlide blankSlide = ppt.createSlide();\r
+\r
+ \r
+ XSLFSlideMaster master = ppt.getSlideMasters()[0];\r
+\r
+ XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE);\r
+ XSLFSlide slide1 = ppt.createSlide(layout1) ;\r
+ XSLFTextShape[] ph1 = slide1.getPlaceholders();\r
+ XSLFTextShape titlePlaceholder1 = ph1[0];\r
+ titlePlaceholder1.setText("This is a title");\r
+ XSLFTextShape subtitlePlaceholder1 = ph1[1];\r
+ subtitlePlaceholder1.setText("this is a subtitle");\r
+\r
+ XSLFSlideLayout layout2 = master.getLayout(SlideLayout.TITLE_AND_CONTENT);\r
+ XSLFSlide slide2 = ppt.createSlide(layout2) ;\r
+ XSLFTextShape[] ph2 = slide2.getPlaceholders();\r
+ XSLFTextShape titlePlaceholder2 = ph2[0];\r
+ titlePlaceholder2.setText("This is a title");\r
+ XSLFTextShape bodyPlaceholder = ph2[1];\r
+ // we are going to add text by paragraphs. Clear the default placehoder text before that\r
+ bodyPlaceholder.clearText();\r
+ XSLFTextParagraph p1 = bodyPlaceholder.addNewTextParagraph();\r
+ p1.setLevel(0);\r
+ p1.addNewTextRun().setText("Level1 text");\r
+ XSLFTextParagraph p2 = bodyPlaceholder.addNewTextParagraph();\r
+ p2.setLevel(1);\r
+ p2.addNewTextRun().setText("Level2 text");\r
+ XSLFTextParagraph p3 = bodyPlaceholder.addNewTextParagraph();\r
+ p3.setLevel(3);\r
+ p3.addNewTextRun().setText("Level3 text");\r
+\r
+ FileOutputStream out = new FileOutputStream("slides.pptx");\r
+ ppt.write(out);\r
+ out.close();\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import java.awt.*;\r
+import java.awt.geom.Rectangle2D;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Basic paragraph and text formatting\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Tutorial2 {\r
+\r
+ public static void main(String[] args) throws IOException{\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+\r
+ XSLFSlide slide1 = ppt.createSlide();\r
+ XSLFTextBox shape1 = slide1.createTextBox();\r
+ // initial height of the text box is 100 pt but\r
+ Rectangle anchor = new Rectangle(10, 100, 300, 100);\r
+ shape1.setAnchor(anchor);\r
+\r
+ XSLFTextParagraph p1 = shape1.addNewTextParagraph();\r
+ XSLFTextRun r1 = p1.addNewTextRun();\r
+ r1.setText("Paragraph Formatting");\r
+ r1.setFontSize(24);\r
+ r1.setFontColor(new Color(85, 142, 213));\r
+\r
+ XSLFTextParagraph p2 = shape1.addNewTextParagraph();\r
+ // If spaceBefore >= 0, then space is a percentage of normal line height.\r
+ // If spaceBefore < 0, the absolute value of linespacing is the spacing in points\r
+ p2.setSpaceBefore(-20); // 20 pt from the previous paragraph\r
+ p2.setSpaceAfter(300); // 3 lines after the paragraph\r
+ XSLFTextRun r2 = p2.addNewTextRun();\r
+ r2.setText("Paragraph properties apply to all text residing within the corresponding paragraph.");\r
+ r2.setFontSize(16);\r
+\r
+ XSLFTextParagraph p3 = shape1.addNewTextParagraph();\r
+\r
+ XSLFTextRun r3 = p3.addNewTextRun();\r
+ r3.setText("Run Formatting");\r
+ r3.setFontSize(24);\r
+ r3.setFontColor(new Color(85, 142, 213));\r
+\r
+ XSLFTextParagraph p4 = shape1.addNewTextParagraph();\r
+ p4.setSpaceBefore(-20); // 20 pt from the previous paragraph\r
+ p4.setSpaceAfter(300); // 3 lines after the paragraph\r
+ XSLFTextRun r4 = p4.addNewTextRun();\r
+ r4.setFontSize(16);\r
+ r4.setText(\r
+ "Run level formatting is the most granular property level and allows " +\r
+ "for the specifying of all low level text properties. The text run is " +\r
+ "what all paragraphs are derived from and thus specifying various " +\r
+ "properties per run will allow for a diversely formatted text paragraph.");\r
+\r
+ // resize the shape to fit text\r
+ shape1.resizeToFitText();\r
+\r
+ FileOutputStream out = new FileOutputStream("text.pptx");\r
+ ppt.write(out);\r
+ out.close();\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import java.awt.*;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * How to set slide title\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Tutorial3 {\r
+\r
+ public static void main(String[] args) throws IOException{\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+\r
+ XSLFSlide slide = ppt.createSlide();\r
+\r
+ XSLFTextShape titleShape = slide.createTextBox();\r
+ titleShape.setPlaceholder(Placeholder.TITLE);\r
+ titleShape.setText("This is a slide title");\r
+ titleShape.setAnchor(new Rectangle(50, 50, 400, 100));\r
+\r
+ FileOutputStream out = new FileOutputStream("title.pptx");\r
+ ppt.write(out);\r
+ out.close();\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import java.awt.*;\r
+import java.awt.geom.Rectangle2D;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * PPTX Tables\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Tutorial4 {\r
+\r
+ public static void main(String[] args) throws IOException{\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+\r
+ // XSLFSlide#createSlide() with no arguments creates a blank slide\r
+ XSLFSlide slide = ppt.createSlide();\r
+\r
+ XSLFTable tbl = slide.createTable();\r
+ tbl.setAnchor(new Rectangle2D.Double(50, 50, 450, 300));\r
+\r
+ int numColumns = 3;\r
+ int numRows = 5;\r
+ XSLFTableRow headerRow = tbl.addRow();\r
+ headerRow.setHeight(50);\r
+ // header\r
+ for(int i = 0; i < numColumns; i++) {\r
+ XSLFTableCell th = headerRow.addCell();\r
+ XSLFTextParagraph p = th.addNewTextParagraph();\r
+ p.setTextAlign(TextAlign.CENTER);\r
+ XSLFTextRun r = p.addNewTextRun();\r
+ r.setText("Header " + (i+1));\r
+ r.setBold(true);\r
+ r.setFontColor(Color.white);\r
+ th.setFillColor(new Color(79, 129, 189));\r
+ th.setBorderBottom(2);\r
+ th.setBorderBottomColor(Color.white);\r
+\r
+ tbl.setColumnWidth(i, 150); // all columns are equally sized\r
+ }\r
+ \r
+ // rows\r
+ \r
+ for(int rownum = 0; rownum < numRows; rownum ++){\r
+ XSLFTableRow tr = tbl.addRow();\r
+ tr.setHeight(50);\r
+ // header\r
+ for(int i = 0; i < numColumns; i++) {\r
+ XSLFTableCell cell = tr.addCell();\r
+ XSLFTextParagraph p = cell.addNewTextParagraph();\r
+ XSLFTextRun r = p.addNewTextRun();\r
+\r
+ r.setText("Cell " + (i+1));\r
+ if(rownum % 2 == 0)\r
+ cell.setFillColor(new Color(208, 216, 232));\r
+ else\r
+ cell.setFillColor(new Color(233, 247, 244));\r
+\r
+ }\r
+ \r
+ }\r
+\r
+\r
+ FileOutputStream out = new FileOutputStream("table.pptx");\r
+ ppt.write(out);\r
+ out.close();\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import org.apache.poi.util.IOUtils;\r
+\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Images\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Tutorial5 {\r
+\r
+ public static void main(String[] args) throws IOException{\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+\r
+ XSLFSlide slide = ppt.createSlide();\r
+ File img = new File(System.getProperty("POI.testdata.path"), "slideshow/clock.jpg");\r
+ byte[] data = IOUtils.toByteArray(new FileInputStream(img));\r
+ int pictureIndex = ppt.addPicture(data, XSLFPictureData.PICTURE_TYPE_PNG);\r
+\r
+ XSLFPictureShape shape = slide.createPicture(pictureIndex);\r
+\r
+ FileOutputStream out = new FileOutputStream("images.pptx");\r
+ ppt.write(out);\r
+ out.close();\r
+ }\r
+}\r
--- /dev/null
+/*\r
+ * ====================================================================\r
+ * Licensed to the Apache Software Foundation (ASF) under one or more\r
+ * contributor license agreements. See the NOTICE file distributed with\r
+ * this work for additional information regarding copyright ownership.\r
+ * The ASF licenses this file to You under the Apache License, Version 2.0\r
+ * (the "License"); you may not use this file except in compliance with\r
+ * the License. You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ * ==================================================================== \r
+ */\r
+\r
+package org.apache.poi.xslf.usermodel;\r
+\r
+import java.awt.*;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Hyperlinks\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Tutorial6 {\r
+\r
+ public static void main(String[] args) throws IOException{\r
+ XMLSlideShow ppt = new XMLSlideShow();\r
+\r
+ XSLFSlide slide1 = ppt.createSlide();\r
+ XSLFSlide slide2 = ppt.createSlide();\r
+\r
+ XSLFTextBox shape1 = slide1.createTextBox();\r
+ shape1.setAnchor(new Rectangle(50, 50, 200, 50));\r
+ XSLFTextRun r1 = shape1.addNewTextParagraph().addNewTextRun();\r
+ XSLFHyperlink link1 = r1.createHyperlink();\r
+ r1.setText("http://poi.apache.org"); // visible text\r
+ link1.setAddress("http://poi.apache.org"); // link address\r
+\r
+ XSLFTextBox shape2 = slide1.createTextBox();\r
+ shape2.setAnchor(new Rectangle(300, 50, 200, 50));\r
+ XSLFTextRun r2 = shape2.addNewTextParagraph().addNewTextRun();\r
+ XSLFHyperlink link2 = r2.createHyperlink();\r
+ r2.setText("Go to the second slide"); // visible text\r
+ link2.setAddress(slide2); // link address\r
+\r
+\r
+\r
+ FileOutputStream out = new FileOutputStream("hyperlinks.pptx");\r
+ ppt.write(out);\r
+ out.close();\r
+ }\r
+}\r
package org.apache.poi.xslf.usermodel;
+import org.apache.poi.POIXMLException;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Units;
import org.apache.poi.xslf.model.PropertyFetcher;
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
*/