解析PPT文件中的图片
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Picture;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.SlideShow;
public class OutputPicture {
// 图片默认存放路径
public final static String path = "F:\ppt";
public static void main(String[] args) throws Exception {
// 加载PPT
HSLFSlideShow _hslf = new HSLFSlideShow("F:\Downloads\myPPT.ppt");
SlideShow _slideShow = new SlideShow(_hslf);
// 获取PPT文件中的图片数据
PictureData[] _pictures = _slideShow.getPictureData();
// 循环读取图片数据
for (int i = 0; i < _pictures.length; i++) {
StringBuilder fileName = new StringBuilder(path);
PictureData pic_data = _pictures[i];
fileName.append(i);
// 设置格式
switch (pic_data.getType()) {
case Picture.JPEG:
fileName.append(".jpg");
break;
case Picture.PNG:
fileName.append(".png");
break;
default:
fileName.append(".data");
}
// 输出文件
FileOutputStream fileOut = new FileOutputStream(new File(fileName.toString()));
fileOut.write(pic_data.getData());
fileOut.close();
}
}
}
在PPT文件中加入外部图片
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;
public class InputPicture {
public static String path = "F:\images\myImage.png";
public static String OUTPUT = "F:\ppt\myppt.ppt";
public static void main(String[] args) throws Exception {
if(args.length != 0){
path = args[0];
}
// 构建PPT
SlideShow _slideShow = new SlideShow();
// 创建幻灯片
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);
// 输出PPT文件
_slideShow.write(new FileOutputStream(new File(OUTPUT)));
}
}
操作文本对象
import java.awt.Color;
import java.awt.Rectangle;
import java.io.FileOutputStream;
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.Slide;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
public class InputTextRun {
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("杜磊米");
_text.setAnchor(new Rectangle(10,10,100,100));
// 创建并置入带有样式的文本
AutoShape _autoShape = new AutoShape(ShapeTypes.Rectangle); //设置形状
TextRun _autoText = _autoShape.createTextRun();
_autoText.setRawText("杜磊米");
_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("F:\ppt\text.ppt"));
}
}
设置各类文件属性
import java.awt.Dimension;
import java.io.FileOutputStream;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.usermodel.SlideShow;
public class PPTProperty {
public static void main(String [] args)throws Exception{
HSLFSlideShow hslf = HSLFSlideShow.create();
SlideShow _slideShow = new SlideShow(hslf);
// 设置页面大小
_slideShow.setPageSize(new Dimension(400,600));
// 设置后创建出相应大小的幻灯片
Slide slide = _slideShow.createSlide();
DocumentSummaryInformation doc = hslf.getDocumentSummaryInformation();
SummaryInformation info = hslf.getSummaryInformation();
doc.setCompany("secret");
info.setAuthor("杜磊米");
info.setTitle("nothing");
// 输出文件
_slideShow.write(new FileOutputStream("F:\ppt\demo.ppt"));
// 完成后, 找到文件 , 右键属性查看:)
}
}