a couple of HSLF examples

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@650130 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2008-04-21 12:52:23 +00:00
parent f5d6d1d2db
commit 182344b2bb
2 changed files with 228 additions and 0 deletions

View File

@ -0,0 +1,148 @@
/* ====================================================================
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.hslf.examples;
import org.apache.poi.hslf.usermodel.*;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Paragraph;
import java.io.*;
/**
* Demonstrates how you can extract misc embedded data from a ppt file
*
* @author Yegor Kozlov
*/
public class DataExtraction {
public static void main(String args[]) throws Exception {
if (args.length == 0) {
usage();
return;
}
FileInputStream is = new FileInputStream(args[0]);
SlideShow ppt = new SlideShow(is);
is.close();
//extract all sound files embedded in this presentation
SoundData[] sound = ppt.getSoundData();
for (int i = 0; i < sound.length; i++) {
String type = sound[i].getSoundType(); //*.wav
String name = sound[i].getSoundName(); //typically file name
byte[] data = sound[i].getData(); //raw bytes
//save the sound on disk
FileOutputStream out = new FileOutputStream(name + type);
out.write(data);
out.close();
}
//extract embedded OLE documents
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
Shape[] shape = slide[i].getShapes();
for (int j = 0; j < shape.length; j++) {
if (shape[j] instanceof OLEShape) {
OLEShape ole = (OLEShape) shape[j];
ObjectData data = ole.getObjectData();
String name = ole.getInstanceName();
if ("Worksheet".equals(name)) {
//save xls on disk
FileOutputStream out = new FileOutputStream(name + "-("+(j)+").xls");
InputStream dis = data.getData();
byte[] chunk = new byte[2048];
int count;
while ((count = dis.read(chunk)) >= 0) {
out.write(chunk,0,count);
}
is.close();
out.close();
} else if ("Document".equals(name)) {
HWPFDocument doc = new HWPFDocument(data.getData());
//read the word document
Range r = doc.getRange();
for(int k = 0; k < r.numParagraphs(); k++) {
Paragraph p = r.getParagraph(k);
System.out.println(p.text());
}
//save on disk
FileOutputStream out = new FileOutputStream(name + "-("+(j)+").doc");
doc.write(out);
out.close();
} else {
System.err.println("Processing " + name);
}
}
}
}
//Pictures
for (int i = 0; i < slide.length; i++) {
Shape[] shape = slide[i].getShapes();
for (int j = 0; j < shape.length; j++) {
if (shape[j] instanceof Picture) {
Picture p = (Picture) shape[j];
PictureData data = p.getPictureData();
String name = p.getPictureName();
int type = data.getType();
String ext;
switch (type) {
case Picture.JPEG:
ext = ".jpg";
break;
case Picture.PNG:
ext = ".png";
break;
case Picture.WMF:
ext = ".wmf";
break;
case Picture.EMF:
ext = ".emf";
break;
case Picture.PICT:
ext = ".pict";
break;
case Picture.DIB:
ext = ".dib";
break;
default:
continue;
}
FileOutputStream out = new FileOutputStream("pict-" + j + ext);
out.write(data.getData());
out.close();
}
}
}
}
private static void usage(){
System.out.println("Usage: DataExtraction ppt");
}
}

View File

@ -0,0 +1,80 @@
/* ====================================================================
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.hslf.examples;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.*;
import java.awt.*;
import java.io.FileOutputStream;
import java.io.FileInputStream;
/**
* Demonstrates how to draw into a slide using the HSLF Graphics2D driver.
*
* @author Yegor Kozlov
*/
public class Graphics2DDemo {
/**
* A simple bar chart demo
*/
public static void main(String[] args) throws Exception {
SlideShow ppt = new SlideShow();
//bar chart data. The first value is the bar color, the second is the width
Object[] def = new Object[]{
Color.yellow, new Integer(40),
Color.green, new Integer(60),
Color.gray, new Integer(30),
Color.red, new Integer(80),
};
Slide slide = ppt.createSlide();
ShapeGroup group = new ShapeGroup();
//define position of the drawing in the slide
Rectangle bounds = new java.awt.Rectangle(200, 100, 350, 300);
group.setAnchor(bounds);
group.setCoordinates(new java.awt.Rectangle(0, 0, 100, 100));
slide.addShape(group);
Graphics2D graphics = new PPGraphics2D(group);
//draw a simple bar graph
int x = 10, y = 10;
graphics.setFont(new Font("Arial", Font.BOLD, 10));
for (int i = 0, idx = 1; i < def.length; i+=2, idx++) {
graphics.setColor(Color.black);
int width = ((Integer)def[i+1]).intValue();
graphics.drawString("Q" + idx, x-5, y+10);
graphics.drawString(width + "%", x + width+3, y + 10);
graphics.setColor((Color)def[i]);
graphics.fill(new Rectangle(x, y, width, 10));
y += 15;
}
graphics.setColor(Color.black);
graphics.setFont(new Font("Arial", Font.BOLD, 14));
graphics.draw(group.getCoordinates());
graphics.drawString("Performance", x + 30, y + 10);
FileOutputStream out = new FileOutputStream("hslf-graphics.ppt");
ppt.write(out);
out.close();
}
}