/**
*
*/
package ppt.utils.commond;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import javax.imageio.ImageIO;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.model.AutoShape;
import org.apache.poi.hslf.model.Line;
import org.apache.poi.hslf.model.ShapeTypes;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
/**
* POI针对PPT的操作示例
*
* @author CAOXIANG
*
*/
public class PPTCommond {
public static String path = "D:\\main_07.png";
public static String OUTPUT = "D:\\test2003.ppt";
/**
* 图片方式
* @param args
* @throws Exception
*/
public static void main1(String[] args) throws Exception {
if (args.length != 0) {
path = args[0];
}
// 构建PPT
SlideShow _slideShow = new SlideShow();
// 设置页面大小
_slideShow.setPageSize(new Dimension(400, 600));
// 创建幻灯片
Slide _slide = _slideShow.createSlide();
// 设置图片类型
int pic_type = -1;
if (path.indexOf(".png") != -1) {
pic_type = Picture.PNG;
} else {
pic_type = Picture.JPEG;
}
File file = new File(path);
BufferedImage image = ImageIO.read(file);
// 新置入图片索引位置
int newIndex = _slideShow.addPicture(file, pic_type);
// 根据索引位置 , 创建图片对象
Picture _picture = new Picture(newIndex);
// 设置图片显示位置
_picture.setAnchor(new Rectangle(100, 100, image.getWidth(), image
.getHeight()));
// 将图片放入幻灯片
_slide.addShape(_picture);
// 可以实现定位幻灯片
Slide _slide_0 = _slideShow.getSlides()[1];
_picture.setAnchor(new Rectangle(200, 200, image.getWidth(), image
.getHeight()));
_slide_0.addShape(_picture);
// 输出PPT文件
_slideShow.write(new FileOutputStream(new File(OUTPUT)));
}
/**
* 文本方式
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
SlideShow _slideShow = new SlideShow();
Slide slide = _slideShow.createSlide();
// 创建并置入简单文本
TextBox _text = new TextBox();
TextRun _textRun = _text.createTextRun();
_textRun.setRawText("XXXXX");
_text.setAnchor(new Rectangle(10, 10, 100, 100));
// 创建并置入带有样式的文本
AutoShape _autoShape = new AutoShape(ShapeTypes.Rectangle); // 设置形状
TextRun _autoText = _autoShape.createTextRun();
_autoText.setRawText("XXXXX");
_autoShape.setAnchor(new Rectangle(200, 200, 100, 100));
_autoShape.setFillColor(new Color(170, 215, 255));
_autoShape.setLineWidth(5.0);
_autoShape.setLineStyle(Line.LINE_DOUBLE);
// AutoShape 对象可以设置多个不同样式文本
TextRun _autoText2 = _autoShape.createTextRun();
RichTextRun _richText = _autoText2.appendText("曹");
_richText.setFontColor(new Color(255, 255, 255));
RichTextRun _richText2 = _autoText2.appendText("翔");
_richText2.setFontColor(new Color(255, 0, 0));
_richText2.setFontSize(12);
// 将文本对象置入幻灯片
slide.addShape(_text);
slide.addShape(_autoShape);
// 输出文件
_slideShow.write(new FileOutputStream(OUTPUT));
}
}