poi-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Karthik.Gopalakrishnan" <kpa...@uky.edu>
Subject Re: Using POI for ppt to image conversion
Date Fri, 19 Sep 2008 16:24:38 GMT

Please find the attached File


/// Here is a copy of the java file i uploaded 

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.AutoShape;
import org.apache.poi.hslf.model.Background;
import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Shape;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;

import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class test {
	static SlideShow ppt;
	static Slide[] slides;
	
	public static void main(String[] args) {
		try {
			ppt = new SlideShow(new HSLFSlideShow("c:\\temp\\ch01.ppt"));
			slides = ppt.getSlides();
			saveTextAsImage(2);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void saveTextAsImage(int slideNumber){
		Background bgnd = slides[slideNumber].getBackground();
		BufferedImage img = new BufferedImage(ppt.getPageSize().width, ppt
				.getPageSize().height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics = img.createGraphics();
		bgnd.draw(graphics);
		Shape[] shapes = slides[slideNumber].getShapes();
		try {
			int j = 0;
			while (j < shapes.length) {
				Rectangle anchor = shapes[j].getAnchor();
				if (shapes[j] instanceof Line) {
					Line line = (Line) shapes[j];
					line.draw(graphics);
				} else if (shapes[j] instanceof AutoShape) {
					AutoShape shape = (AutoShape) shapes[j];
					TextBox textBoxShape = new TextBox();
					Slide newSlide = ppt.createSlide();
					newSlide.addShape(textBoxShape);
					textBoxShape.setText("January\r" + "February\r" + "March\r"
							+ "April");
					RichTextRun rt = textBoxShape.getTextRun().getRichTextRuns()[0];
					rt.setFontSize(42);
					rt.setBullet(true);
					rt.setBulletOffset(0);
					rt.setTextOffset(50); 
					rt.setBulletChar('\u263A');
					textBoxShape.setAnchor(anchor);
					textBoxShape.draw(graphics);
					writeToFile("slide-" + slideNumber+".jpeg", img);
				} 
				j++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void writeToFile(String fileName, BufferedImage img) {
		try {
			File file = new File("c:\\temp\\" + fileName);
			FileOutputStream out;
			out = new FileOutputStream(file);
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
			com.sun.image.codec.jpeg.JPEGEncodeParam param = encoder
					.getDefaultJPEGEncodeParam(img);
			param.setQuality(0.7f, true);
			encoder.setJPEGEncodeParam(param);
			encoder.encode(img);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ImageFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}

-- 
View this message in context: http://www.nabble.com/Using-POI-for-ppt-to-image-conversion-tp19555971p19575440.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@poi.apache.org
For additional commands, e-mail: user-help@poi.apache.org


Mime
View raw message