java poi ppt操作示例
poi3.9版本,官网 http://poi.apache.org/slideshow/how-to-shapes.html
import java.awt.Color;
import java.io.File;
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.Fill;
import org.apache.poi.hslf.model.Freeform;
import org.apache.poi.hslf.model.HeadersFooters;
import org.apache.poi.hslf.model.Hyperlink;
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.ShapeTypes;
import org.apache.poi.hslf.model.Slide;
import org.apache.poi.hslf.model.SlideMaster;
import org.apache.poi.hslf.model.Table;
import org.apache.poi.hslf.model.TableCell;
import org.apache.poi.hslf.model.TextBox;
import org.apache.poi.hslf.model.TextRun;
import org.apache.poi.hslf.usermodel.PictureData;
import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.hslf.usermodel.SoundData; public class PPTParseUtil {
public static void main(String[] args) throws IOException {
SlideShow ppt = new SlideShow(); // 设置标题,底部信息
// presentation-scope headers / footers
HeadersFooters hdd = ppt.getSlideHeadersFooters();
hdd.setSlideNumberVisible(true);
hdd.setFootersText("Created by POI-HSLF"); // add first slide
Slide s1 = ppt.createSlide(); // add second slide
Slide s2 = ppt.createSlide();
// retrieve page size. Coordinates are expressed in points (72 dpi)
java.awt.Dimension pgsize = ppt.getPageSize();
int pgx = pgsize.width; // slide width
int pgy = pgsize.height; // slide height // set new page size
ppt.setPageSize(new java.awt.Dimension(1024, 768));
// save changes
FileOutputStream out = new FileOutputStream("E:\\logs\\slideshow.ppt"); // get slides
Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) {
Shape[] sh = slide[i].getShapes();
for (int j = 0; j < sh.length; j++) {
// name of the shape
String name = sh[j].getShapeName(); // shapes's anchor which defines the position of this shape in
// the slide
java.awt.Rectangle anchor = sh[j].getAnchor(); if (sh[j] instanceof Line) {
Line line = (Line) sh[j];
// work with Line
} else if (sh[j] instanceof AutoShape) {
AutoShape shape = (AutoShape) sh[j];
// work with AutoShape
} else if (sh[j] instanceof TextBox) {
TextBox shape = (TextBox) sh[j];
// work with TextBox
} else if (sh[j] instanceof Picture) {
Picture shape = (Picture) sh[j];
// work with Picture
}
}
} // Drawing a shape on a slide
Slide slide2 = ppt.createSlide(); // set slide title
TextBox title = slide2.addTitle();
title.setText("Hello, World!");
// Line shape
Line line = new Line();
line.setAnchor(new java.awt.Rectangle(50, 50, 100, 20));
line.setLineColor(new Color(0, 128, 0));
line.setLineStyle(Line.LINE_DOUBLE);
slide2.addShape(line); // TextBox
TextBox txt = new TextBox();
txt.setText("Hello, World!");
txt.setAnchor(new java.awt.Rectangle(300, 100, 300, 50)); // use RichTextRun to work with the text format
RichTextRun rt = txt.getTextRun().getRichTextRuns()[0];
rt.setFontSize(32);
rt.setFontName("Arial");
rt.setBold(true);
rt.setItalic(true);
rt.setUnderlined(true);
rt.setFontColor(Color.red);
rt.setAlignment(TextBox.AlignRight); slide2.addShape(txt); // create shapes of arbitrary geometry
java.awt.geom.GeneralPath path = new java.awt.geom.GeneralPath();
path.moveTo(100, 100);
path.lineTo(200, 100);
path.curveTo(50, 45, 134, 22, 78, 133);
path.curveTo(10, 45, 134, 56, 78, 100);
path.lineTo(100, 200);
path.closePath(); Freeform shape = new Freeform();
shape.setPath(path);
slide2.addShape(shape); // Autoshape
// 32-point star
AutoShape sh1 = new AutoShape(ShapeTypes.Star32);
sh1.setAnchor(new java.awt.Rectangle(50, 50, 100, 200));
sh1.setFillColor(Color.red);
slide2.addShape(sh1); // Trapezoid
AutoShape sh2 = new AutoShape(ShapeTypes.Trapezoid);
sh2.setAnchor(new java.awt.Rectangle(150, 150, 100, 200));
sh2.setFillColor(Color.blue);
slide2.addShape(sh2); // work with pictures
// extract all pictures contained in the presentation
PictureData[] pdata = ppt.getPictureData();
for (int ii = 0; ii < pdata.length; ii++) {
PictureData pict = pdata[ii]; // picture data
byte[] data = pict.getData(); int type = pict.getType();
String ext;
switch (type) {
case Picture.JPEG:
ext = ".jpg";
break;
case Picture.PNG:
ext = ".png";
break;
case Picture.WMF:
ext = ".wmf";
break;
case Picture.EMF:
ext = ".emf";
break;
case Picture.PICT:
ext = ".pict";
break;
default:
continue;
}
FileOutputStream out2 = new FileOutputStream("pict_" + ii + ext);
out2.write(data);
out2.close(); } // add a new picture to this slideshow and insert it in a new slide
int idx = ppt.addPicture(new File("E:\\logs\\clock.png"), Picture.PNG); Picture pict = new Picture(idx); // set image position in the slide
pict.setAnchor(new java.awt.Rectangle(100, 100, 300, 200)); Slide slide3 = ppt.createSlide();
slide3.addShape(pict); // This slide has its own background.
// Without this line it will use master's background.
slide3.setFollowMasterBackground(false);
Fill fill = slide3.getBackground().getFill();
int idx1 = ppt.addPicture(new File("E:\\logs\\clock.png"), Picture.PNG);
fill.setFillType(Fill.FILL_PATTERN);
fill.setPictureData(idx1); // create bulleted lists TextBox shape1 = new TextBox();
RichTextRun rt1 = shape1.getTextRun().getRichTextRuns()[0];
shape1.setText("January\r" + "February\r" + "March\r" + "April");
rt1.setFontSize(42);
rt1.setBullet(true);
rt1.setBulletOffset(0); // bullet offset
rt1.setTextOffset(50); // text offset (should be greater than bullet
// offset)
rt1.setBulletChar('\u263A'); // bullet character
slide3.addShape(shape1); shape1.setAnchor(new java.awt.Rectangle(50, 50, 500, 300)); // position
// of the
// text box
// in the
// slide
slide3.addShape(shape1); // now retrieve pictures containes in the first slide and save them on
// disk
slide3 = ppt.getSlides()[0];
Shape[] sh3 = slide3.getShapes();
for (int i2 = 0; i2 < sh3.length; i2++) {
if (sh3[i2] instanceof Picture) {
Picture pict1 = (Picture) sh3[i2];
PictureData pictData = pict1.getPictureData();
byte[] data = pictData.getData();
int type = pictData.getType();
if (type == Picture.JPEG) {
FileOutputStream out3 = new FileOutputStream("slide0_" + i2
+ ".jpg");
out3.write(data);
out3.close();
} else if (type == Picture.PNG) {
FileOutputStream out4 = new FileOutputStream("slide0_" + i2
+ ".png");
out4.write(data);
out4.close();
}
}
} // modify background of a slide master
SlideMaster master = ppt.getSlidesMasters()[0]; Fill fill1 = master.getBackground().getFill();
int idx11 = ppt
.addPicture(new File("E:\\logs\\clock.png"), Picture.PNG);
fill1.setFillType(Fill.FILL_PICTURE);
fill1.setPictureData(idx11); // read hyperlinks from a slide show
Slide[] slide1 = ppt.getSlides();
for (int j = 0; j < slide1.length; j++) { // read hyperlinks from the text runs
TextRun[] txt1 = slide1[j].getTextRuns();
if (txt1 == null || txt1.length == 0) {
continue;
}
for (int k = 0; k < txt1.length; k++) {
String text = txt1[k].getText();
Hyperlink[] links = txt1[k].getHyperlinks();
if (links != null)
for (int l = 0; l < links.length; l++) {
Hyperlink link = links[l];
String title1 = link.getTitle();
String address = link.getAddress();
String substring = text.substring(link.getStartIndex(),
link.getEndIndex() - 1); // in ppt end index is
// inclusive
System.out.println(title1 + address + substring);
}
} // in PowerPoint you can assign a hyperlink to a shape without text,
// for example to a Line object. The code below demonstrates how to
// read such hyperlinks
Shape[] sh = slide1[j].getShapes();
for (int k = 0; k < sh.length; k++) {
Hyperlink link = sh[k].getHyperlink();
if (link != null) {
String title1 = link.getTitle();
String address = link.getAddress();
System.out.println(title1 + address);
}
}
} // table data
String[][] data = { { "INPUT FILE", "NUMBER OF RECORDS" },
{ "Item File", "11,559" }, { "Vendor File", "300" },
{ "Purchase History File", "10,000" },
{ "Total # of requisitions", "10,200,038" } }; // 创建表格
Slide slide11 = ppt.createSlide();
// create a table of 5 rows and 2 columns
Table table = new Table(5, 2);
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
TableCell cell = table.getCell(i, j);
cell.setText(data[i][j]); RichTextRun rt11 = cell.getTextRun().getRichTextRuns()[0];
rt11.setFontName("Arial");
rt11.setFontSize(10); cell.setVerticalAlignment(TextBox.AnchorMiddle);
cell.setHorizontalAlignment(TextBox.AlignCenter);
}
} // set table borders
Line border = table.createBorder();
border.setLineColor(Color.black);
border.setLineWidth(1.0);
table.setAllBorders(border); // set width of the 1st column
table.setColumnWidth(0, 300);
// set width of the 2nd column
table.setColumnWidth(1, 150); slide11.addShape(table);
table.moveTo(100, 100); // retrieve embedded sounds 获取语音信息
SoundData[] sound = ppt.getSoundData();
for (int i = 0; i < sound.length; i++) {
// save *WAV sounds on disk
if (sound[i].getSoundType().equals(".WAV")) {
FileOutputStream out1 = new FileOutputStream(
sound[i].getSoundName());
out1.write(sound[i].getData());
out1.close();
}
} ppt.write(out);
out.close();
}
}
本文转自:http://blog.csdn.net/zhongweijian/article/details/8299531
java poi ppt操作示例的更多相关文章
- java POI创建Excel示例(xslx和xsl区别 )
Java用来处理office类库有很多,其中POI就是比较出名的一个,它是apache的类库,现在版本到了3.10,也就是2014年2月8号这个版本. 在处理PPT,Excel和Word前,需要导入以 ...
- java poi ppt 接口的基本操作
依赖 在 pom.xml中增加以下依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId& ...
- zookeeper Java API 简单操作示例
本文主要介绍如何在java IDE中如何应用使用客户端与zookeeper服务器通信. 首先搭建maven环境,并在pom文件中加入zookeeper引用包: <!-- https://mvnr ...
- java poi excel操作 把第一列放到最后去
@Override public void adjustExcleColumnPosition(String filePath,int col) throws Exception{ File file ...
- java poi excel操作 下拉菜单 及数据有效性
1 private InputStream updateTemplateStyleHSSF(InputStream inputStream,CsCustCon csCustCon) throws IO ...
- java poi 操作ppt
java poi 操作ppt 可以参考: https://www.w3cschool.cn/apache_poi_ppt/apache_poi_ppt_installation.html http:/ ...
- 在java poi导入Excel通用工具类示例详解
转: 在java poi导入Excel通用工具类示例详解 更新时间:2017年09月10日 14:21:36 作者:daochuwenziyao 我要评论 这篇文章主要给大家介绍了关于在j ...
- 在Eclipse中运行JAVA代码远程操作HBase的示例
在Eclipse中运行JAVA代码远程操作HBase的示例 分类: 大数据 2014-03-04 13:47 3762人阅读 评论(2) 收藏 举报 下面是一个在Windows的Eclipse中通过J ...
- java用org.apache.poi包操作excel
一.POI简介 Jakarta POI 是apache的子项目,目标是处理ole2对象.它提供了一组操纵Windows文档的Java API 目前比较成熟的是HSSF接口,处理MS Excel(97- ...
随机推荐
- android studio常见错误
1.Failed to import new Gradle project: Could not install Gradle distribution from'http://services.gr ...
- NS2中trace文件分析
ns中模拟出来的时间最终会以trace文件的形式告诉我们,虽然说一般都是用awk等工具分析trace文件,但是了解trace文件的格式也是必不可少的.下面就介绍一下无线网络模拟中trace文件的格式. ...
- VMWARE虚拟机CentOS6.4系统使用主机无线网卡上网的三种方法介绍
转自:http://www.jb51.net/network/98820.html 如何真正的实现VMWARE虚拟机CentOS6.4系统使用主机无线网卡上网 环境:WIN7旗舰版,台式机,U盘无 ...
- loj 1377 (bfs)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1377 思路:这道题只要处理好遇到"*"这种情况就可以搞定了.我们可 ...
- hdu 4114(状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...
- Android px、dp、sp之间相互转换
dp(dip): device independent pixels(设备独立像素). 不同设备有不同的显示效果,这个和设备硬件有关,一般我们为了支持WVGA.HVGA和QVGA 推荐使用这个,不依赖 ...
- C语言补码作用
补码主要是为了cpu运算器在进行减法运算时避免借位而设立的. 在早期,cpu中的运算器部分,只要实现一个加法器就可以完成四由算术运算. 因为计算机中的数值编码是有限位数的,所以减法实际上相当于加上减数 ...
- JDK 伪异步编程(线程池)
伪异步IO编程 BIO主要的问题在于每当有一个新的客户端请求接入时,服务端必须创建一个新的线程处理新接入的客户端链路,一个线程只能处理一个客户端连接.在高性能服务器应用领域,往往需要面向成千上万个客户 ...
- DuckHunter Attacks
DuckHunter Attacks DuckHunter Attacks是从USB Rubber Ducky (USB橡皮鸭)发展过来的HID攻击方式.USB Rubber Ducky是从201 ...
- CDN(内容分发网络)是什么?
尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.其目的是使用户可就近取得所需内容,解决Internet网络拥挤的状况,提高用户访问网站的响应速度. 解决CDN缓 ...