1)如果是创建新的PPT文档,直接使用SlideShow和Slide类就可以,其中SlideShow表示PPT文档,Slide表示某一张幻灯片
如下代码创建空的PPT文档:
SlideShow ppt = new SlideShow();
Slide[] slides = ppt.getSlides();
assertTrue(slides.length == 0);
savePPTFile(ppt);
private void savePPTFile(SlideShow ppt) throws Exception{
FileOutputStream out = new FileOutputStream(\"ppt测试.ppt\");
ppt.write(out);
out.close();
}
2)设置母版,这样后续的新建幻灯片都将使用母版的字体,背景等设置
SlideShow ppt = new SlideShow();
//设置幻灯片大小
ppt.setPageSize(new Dimension(760,600));
SlideMaster master = ppt.getSlidesMasters()[0];
//设置母板背景,支持多种图片格式
int picIndex = ppt.addPicture(new File(\"background.png\"), Picture.PNG);
Picture background = new Picture(picIndex);
//设置图片位置
background.setAnchor(new java.awt.Rectangle(0, 0, ppt.getPageSize().width
, ppt.getPageSize().height));
master.addShape(background);
3)创建幻灯片并插入文本
SlideShow ppt = new SlideShow();
Slide newSlide = ppt.createSlide();
//添加幻灯片标题
TextBox title = newSlide.addTitle();
RichTextRun titleRun = title.getTextRun().getRichTextRuns()[0];
titleRun.setFontColor(Color.RED);
title.setText(\"ppt测试\");
//添加文本框
TextBox txt = new TextBox();
RichTextRun richTextRun = txt.getTextRun().getRichTextRuns()[0];
richTextRun.setFontColor(Color.BLUE);
//setText参数字符串可以包含回车、换行符,但是最后一行不能以\\r\\n结尾,否则设置的格式没有效果(v3.5)
richTextRun.setText(\"这里可以换行\\r\\n第二行文本\");
txt.setAnchor(new java.awt.Rectangle(50,150,400,400));
newSlide.addShape(txt);
savePPTFile(ppt);
4)插入图片,支持多种格式
SlideShow ppt = new SlideShow();
Slide newSlide = ppt.createSlide();
int picIndex = ppt.addPicture(new File(\"图片.jpg\"), Picture.JPEG);
Picture jpg = new Picture(picIndex);
//set image position in the slide
jpg.setAnchor(new java.awt.Rectangle(360, 200, 280, 260));
newSlide.addShape(jpg);
savePPTFile(ppt);
5)插入表格(v3.5)
SlideShow ppt = new SlideShow();
Slide slide = ppt.createSlide();
String[][] datas = {
{\"序号\", \"姓名\",\"年龄\"},
{\"1\", \"张三\",\"30\"},
{\"2\", \"李四\",\"27\"},
};
//create a table of 3 rows and 3 columns
Table table = new Table(3, 3);
for (int i = 0; i < datas.length; i++) {
for (int j = 0; j < datas[i].length; j++) {
TableCell cell = table.getCell(i, j);
RichTextRun rt = cell.getTextRun().getRichTextRuns()[0];
rt.setFontName(\"宋体\");
rt.setFontSize(12);
cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
cell.setText(datas[i][j]);
if(i == 0){//首行背景设置为灰色
cell.setFillColor(Color.GRAY);
}
}
}
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(2.0);
table.setAllBorders(border);
slide.addShape(table);
table.moveTo(160,260);
savePPTFile(ppt);
6)如果是读取已存在的PPT文档则还要用到HSLFSlideShow,下面代码将PPT文件导出为图片(png)格式,如果幻灯片上有中文字符则这些字符的字体需要修改为支持中文的字体(宋体等),否则导出的图片的中文字符不能正常显示
SlideShow ppt = new SlideShow(new HSLFSlideShow(\"PPT测试.ppt\"));
Dimension pgsize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height
, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
//clear the drawing area
graphics.setPaint(Color.white);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
//render
slide[i].draw(graphics);
FileOutputStream out = new FileOutputStream(\"slide-\" + (i+1) + \".png\");
javax.imageio.ImageIO.write(img, \"png\", out);
out.close();
}
7)提取PPT文档信息
SlideShow ppt = new SlideShow(new HSLFSlideShow(\"PPT测试.ppt\"));
Slide[] slides = ppt.getSlides();
//提取文本信息
for (Slide each : slides) {
System.out.println(each.getTitle()) ;
TextRun[] textRuns = each.getTextRuns();
for (int i=0 ;i< textRuns.length; i++ ) {
System.out.println(textRuns[i].getText());
RichTextRun[] richTextRuns = textRuns[i].getRichTextRuns();
for (int j = 0; j < richTextRuns.length; j++) {
System.out.println(richTextRuns[j].getText());
}
}
}
//提取所有JPEG图片
PictureData[] picDatas = ppt.getPictureData();
for (int i=0;i<picDatas.length;i++) {
if(picDatas[i].getType() == Picture.JPEG){
FileOutputStream out = new FileOutputStream(\"jpg_\" + i + \".jpg\");
ppt.write(out);
out.close();
}
}
8)设置PPT文档摘要信息(文档点击鼠标右键查看属性)
HSLFSlideShow hslf = HSLFSlideShow.create();
DocumentSummaryInformation dsi= hslf.getDocumentSummaryInformation();
SummaryInformation si= hslf.getSummaryInformation();
dsi.setCompany(\"yourCompany\");
dsi.setCategory(\"ppt测试\");
si.setAuthor(\"yourName\");
si.setTitle(\"标题\");
SlideShow ppt = new SlideShow(hslf);
savePPTFile(ppt);