ppt转jpg并获取标题
获取每个ppt的标题,如果没有标题,则显示为第几页
public List<String> getDocument(InputStream is, String filename) {
List<String> keyList = new ArrayList<String>();
try {
SlideShow ss = new SlideShow(new HSLFSlideShow(is));// is
// 为文件的InputStream,建立SlideShow
Dimension pagesize = ss.getPageSize();//获取PPT的页数
Slide[] slides = ss.getSlides();// 获得每一张幻灯片
for (int i = 0; i < slides.length; i++) {
TextRun[] t = slides[i].getTextRuns();// 为了取得幻灯片的文字内容,建立TextRun
for (int j = 0; j < t.length; j++) {
RichTextRun[] rtruns = t[j].getRichTextRuns();
for (int l = 0; l < rtruns.length; l++) {
int index = rtruns[l].getFontIndex();
String name = rtruns[l].getFontName();
getsavefilename rtruns[l].setFontIndex(1);
rtruns[l].setFontName("宋体");
}
}
if(null != slides[i].getTitle() && !"".equals(slides[i].getTitle())){
keyList.add(slides[i].getTitle().trim());
}else {
if(i == 0){
keyList.add("首页");
}else {
keyList.add("第" + (i + 1) + "页");
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return keyList;
}
将ppt另存为,保存为一张张图片,图片名称为幻灯片xx
public static final int ppSaveAsJPG = 17;
private ActiveXComponent ppt;
private ActiveXComponent presentation;
public JacobPptUtil(String filePath,boolean isVisble)throws Exception{
if (null == filePath || "".equals(filePath)) {
throw new Exception("文件路径为空!");
}
File file = new File(filePath);
if (!ists()) {
throw new Exception("文件不存在!");
}
ComThread.InitSTA();//初始化com的线程,非常重要,否则第二次创建com对象的时候会出现can’t co-create object异常
ppt = new ActiveXComponent("PowerPoint.Application");
setIsVisble(ppt, isVisble);
// 打开一个现有的 Presentation 对象
ActiveXComponent presentations = ppt.getPropertyAsComponent("Presentations");
presentation = presentations.invokeGetComponent("Open", new Variant(filePath), n
ew Variant(true));
}
/**
* 将ppt转化为图片
*
* @param pptfile
* @param saveToFolder
* @author liwx
*/
public void PPTToJPG(String pptfile, String saveToFolder) {
try {
saveAs(presentation, saveToFolder, ppSaveAsJPG);
if (presentation != null) {
Dispatch.call(presentation, "Close");
presentation=null;
}
} catch (Exception e) {
e.printStackTrace();
ComThread.Release();
} finally {
if (presentation != null) {
Dispatch.call(presentation, "Close");
}
ppt.invoke("Quit", new Variant[] {});
ComThread.Release(); //释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
}
}
/**
* ppt另存为
*
* @param presentation
* @param saveTo
* @param ppSaveAsFileType
* @date 2009-7-4
* @author YHY
*/
public void saveAs(Dispatch presentation, String saveTo,
int ppSaveAsFileType)throws Exception {
Dispatch.call(presentation, "SaveAs", saveTo, new Variant(
ppSaveAsFileType));
}
/**
* 设置是否可见
* @param visble
* @param obj
*/
private void setIsVisble(Dispatch obj,boolean visble)throws Exception{
Dispatch.put(obj, "Visible", new Variant(visble));
}
架包poi-scratchpad-3.8-20120326.jar,jacob.jar,jacob.dll
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论