]> source.dussan.org Git - poi.git/commitdiff
a couple of HSLF examples
authorYegor Kozlov <yegor@apache.org>
Mon, 21 Apr 2008 12:52:23 +0000 (12:52 +0000)
committerYegor Kozlov <yegor@apache.org>
Mon, 21 Apr 2008 12:52:23 +0000 (12:52 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@650130 13f79535-47bb-0310-9956-ffa450edef68

src/scratchpad/examples/src/org/apache/poi/hslf/examples/DataExtraction.java [new file with mode: 0755]
src/scratchpad/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java [new file with mode: 0755]

diff --git a/src/scratchpad/examples/src/org/apache/poi/hslf/examples/DataExtraction.java b/src/scratchpad/examples/src/org/apache/poi/hslf/examples/DataExtraction.java
new file mode 100755 (executable)
index 0000000..05bf824
--- /dev/null
@@ -0,0 +1,148 @@
+\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
+package org.apache.poi.hslf.examples;\r
+\r
+import org.apache.poi.hslf.usermodel.*;\r
+import org.apache.poi.hslf.model.*;\r
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
+import org.apache.poi.hwpf.HWPFDocument;\r
+import org.apache.poi.hwpf.usermodel.Range;\r
+import org.apache.poi.hwpf.usermodel.Paragraph;\r
+\r
+import java.io.*;\r
+\r
+/**\r
+ * Demonstrates how you can extract misc embedded data from a ppt file\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class DataExtraction {\r
+\r
+    public static void main(String args[]) throws Exception {\r
+\r
+        if (args.length == 0) {\r
+            usage();\r
+            return;\r
+        }\r
+\r
+        FileInputStream is = new FileInputStream(args[0]);\r
+        SlideShow ppt = new SlideShow(is);\r
+        is.close();\r
+\r
+        //extract all sound files embedded in this presentation\r
+        SoundData[] sound = ppt.getSoundData();\r
+        for (int i = 0; i < sound.length; i++) {\r
+            String type = sound[i].getSoundType();  //*.wav\r
+            String name = sound[i].getSoundName();  //typically file name\r
+            byte[] data = sound[i].getData();       //raw bytes\r
+\r
+            //save the sound  on disk\r
+            FileOutputStream out = new FileOutputStream(name + type);\r
+            out.write(data);\r
+            out.close();\r
+        }\r
+\r
+        //extract embedded OLE documents\r
+        Slide[] slide = ppt.getSlides();\r
+        for (int i = 0; i < slide.length; i++) {\r
+            Shape[] shape = slide[i].getShapes();\r
+            for (int j = 0; j < shape.length; j++) {\r
+                if (shape[j] instanceof OLEShape) {\r
+                    OLEShape ole = (OLEShape) shape[j];\r
+                    ObjectData data = ole.getObjectData();\r
+                    String name = ole.getInstanceName();\r
+                    if ("Worksheet".equals(name)) {\r
+\r
+                        //save xls on disk\r
+                        FileOutputStream out = new FileOutputStream(name + "-("+(j)+").xls");\r
+                        InputStream dis = data.getData();\r
+                        byte[] chunk = new byte[2048];\r
+                        int count;\r
+                        while ((count = dis.read(chunk)) >= 0) {\r
+                          out.write(chunk,0,count);\r
+                        }\r
+                        is.close();\r
+                        out.close();\r
+                    } else if ("Document".equals(name)) {\r
+                        HWPFDocument doc = new HWPFDocument(data.getData());\r
+                        //read the word document\r
+                        Range r = doc.getRange();      \r
+                        for(int k = 0; k < r.numParagraphs(); k++) {\r
+                            Paragraph p = r.getParagraph(k);\r
+                            System.out.println(p.text());\r
+                         }\r
+\r
+                        //save on disk\r
+                        FileOutputStream out = new FileOutputStream(name + "-("+(j)+").doc");\r
+                        doc.write(out);\r
+                        out.close();\r
+                     }  else {\r
+                        System.err.println("Processing " + name);\r
+                    }\r
+                }\r
+\r
+            }\r
+        }\r
+\r
+        //Pictures\r
+        for (int i = 0; i < slide.length; i++) {\r
+            Shape[] shape = slide[i].getShapes();\r
+            for (int j = 0; j < shape.length; j++) {\r
+                if (shape[j] instanceof Picture) {\r
+                    Picture p = (Picture) shape[j];\r
+                    PictureData data = p.getPictureData();\r
+                    String name = p.getPictureName();\r
+                    int type = data.getType();\r
+                    String ext;\r
+                    switch (type) {\r
+                        case Picture.JPEG:\r
+                            ext = ".jpg";\r
+                            break;\r
+                        case Picture.PNG:\r
+                            ext = ".png";\r
+                            break;\r
+                        case Picture.WMF:\r
+                            ext = ".wmf";\r
+                            break;\r
+                        case Picture.EMF:\r
+                            ext = ".emf";\r
+                            break;\r
+                        case Picture.PICT:\r
+                            ext = ".pict";\r
+                            break;\r
+                        case Picture.DIB:\r
+                            ext = ".dib";\r
+                            break;\r
+                        default:\r
+                            continue;\r
+                    }\r
+                    FileOutputStream out = new FileOutputStream("pict-" + j + ext);\r
+                    out.write(data.getData());\r
+                    out.close();\r
+                }\r
+\r
+            }\r
+        }\r
+\r
+    }\r
+\r
+    private static void usage(){\r
+        System.out.println("Usage: DataExtraction  ppt");\r
+    }\r
+}\r
diff --git a/src/scratchpad/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java b/src/scratchpad/examples/src/org/apache/poi/hslf/examples/Graphics2DDemo.java
new file mode 100755 (executable)
index 0000000..87a59c3
--- /dev/null
@@ -0,0 +1,80 @@
+\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
+package org.apache.poi.hslf.examples;\r
+\r
+import org.apache.poi.hslf.usermodel.SlideShow;\r
+import org.apache.poi.hslf.model.*;\r
+\r
+import java.awt.*;\r
+import java.io.FileOutputStream;\r
+import java.io.FileInputStream;\r
+\r
+/**\r
+ * Demonstrates how to draw into a slide using the HSLF Graphics2D driver.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Graphics2DDemo {\r
+\r
+    /**\r
+     * A simple bar chart demo\r
+     */\r
+    public static void main(String[] args) throws Exception {\r
+        SlideShow ppt = new SlideShow();\r
+\r
+        //bar chart data. The first value is the bar color, the second is the width\r
+        Object[] def = new Object[]{\r
+            Color.yellow, new Integer(40),\r
+            Color.green, new Integer(60),\r
+            Color.gray, new Integer(30),\r
+            Color.red, new Integer(80),\r
+        };\r
+\r
+        Slide slide = ppt.createSlide();\r
+\r
+        ShapeGroup group = new ShapeGroup();\r
+        //define position of the drawing in the slide\r
+        Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);\r
+        group.setAnchor(bounds);\r
+        group.setCoordinates(new java.awt.Rectangle(0, 0, 100, 100));\r
+        slide.addShape(group);\r
+        Graphics2D graphics = new PPGraphics2D(group);\r
+\r
+        //draw a simple bar graph\r
+        int x = 10, y = 10;\r
+        graphics.setFont(new Font("Arial", Font.BOLD, 10));\r
+        for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {\r
+            graphics.setColor(Color.black);\r
+            int width = ((Integer)def[i+1]).intValue();\r
+            graphics.drawString("Q" + idx, x-5, y+10);\r
+            graphics.drawString(width + "%", x + width+3, y + 10);\r
+            graphics.setColor((Color)def[i]);\r
+            graphics.fill(new Rectangle(x, y, width, 10));\r
+            y += 15;\r
+        }\r
+        graphics.setColor(Color.black);\r
+        graphics.setFont(new Font("Arial", Font.BOLD, 14));\r
+        graphics.draw(group.getCoordinates());\r
+        graphics.drawString("Performance", x + 30, y + 10);\r
+\r
+        FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");\r
+        ppt.write(out);\r
+        out.close();\r
+    }\r
+\r
+}\r