POI操作PPT

package com.castle.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));

}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值