如何利用poi将ppt,pptx转换为HTML
大部分内容是参考网上的,还有一部分是自己写的,毕竟是忙了很长时间,分享下劳动成果,代码很简单,主要思路是将ppt或者pptx,解析成图片,然后拼装成html代码字符串
说明:方法中传了两个参数,一个是ppt文件在服务器的物理路径,一个是相对于项目的路径,其实是一个地址,相对于项目的路径主要是为了生成存放图片的文件夹。每上传一个文件,如果只是用于预览,生成的文件未免有些多了,建议还是定期删除为好。
package com.ysy.officeRead.controller;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hslf.usermodel.HSLFSlide;
import org.apache.poi.hslf.usermodel.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.HSLFTextParagraph;
import org.apache.poi.hslf.usermodel.HSLFTextRun;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
public class PPTToImage {
public static void main(String[] args) {
File file = new File("D:/222.ppt");
String projectPath = "";
doPPtoImage(file,projectPath);
}
//用来解析ppt文件,生成html
public static String doPPTToString(String url,String projectPath){
File file = new File(url);
String proString2 = projectPath.substring(0, projectPath.lastIndexOf("."))+"/";
String pathString = url.substring(0, url.lastIndexOf("."));
if(file.exists()){
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
return null;
}
try {
List<String> imgList = new ArrayList<String>();
FileInputStream is = new FileInputStream(file);
HSLFSlideShow hss = new HSLFSlideShow(is);
is.close();
Dimension pgsize = hss.getPageSize();
List<HSLFSlide> slides = hss.getSlides();
for (int i = 0; i < slides.size(); i++) {
System.out.println("第"+i+"页");
List<List<HSLFTextParagraph>> sTextRun = slides.get(i).getTextParagraphs();
for (int j = 0; j < sTextRun.size(); j++) {
List<HSLFTextParagraph> htps = sTextRun.get(j);
for (int k = 0; k < htps.size(); k++) {
List<HSLFTextRun> htr = htps.get(k).getTextRuns();
for (int l = 0; l < htr.size(); l++) {
htr.get(l).setFontIndex(1);
htr.get(l).setFontFamily("宋体");
System.out.println(htr.get(l).getRawText());
}
}
}
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setColor(java.awt.Color.BLUE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slides.get(i).draw(graphics);
//这里设置图片的存放路径和图片的格式(jpeg,png,bmp等),注意生成文件路径
File imgFile = new File(pathString+"/"+(i+1)+".jpeg");
if (!imgFile.exists()) {
if(!imgFile.isDirectory()){
imgFile.getParentFile().mkdirs();
}
imgFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(pathString+"/"+(i+1)+".jpeg");
imgList.add(proString2+(i+1)+".jpeg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
String imgcontentString = "";
//遍历列表,循环输出图片
if(imgList.size()>0){
for (String string : imgList) {
imgcontentString = imgcontentString
+ "<p valign='middle' align='center'><img src='"+string+"' width='326.95pt' height='266.95pt' /></p></br>";
}
}
System.out.println("success!!");
return imgcontentString;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return null;
}
//用来解析pptx,生成html
public static String doPPTXToString(String url,String projectPath){
File file = new File(url);
String proString2 = projectPath.substring(0, projectPath.lastIndexOf("."))+"/";
String pathString = url.substring(0, url.lastIndexOf("."));
if(file.exists()){
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
return null;
}
try {
List<String> imgList = new ArrayList<String>();
FileInputStream is = new FileInputStream(file);
XMLSlideShow hss = new XMLSlideShow(is);
is.close();
List<XSLFSlide> slides = hss.getSlides();
for (int i = 0; i < slides.size(); i++) {
System.out.println("第"+i+"页");
Dimension pgsize = hss.getPageSize();
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setColor(java.awt.Color.WHITE);
graphics.fillRect(0, 0, pgsize.width, pgsize.height);
/*graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));*/
slides.get(i).draw(graphics);
//这里设置图片的存放路径和图片的格式(jpeg,png,bmp等),注意生成文件路径
File imgFile = new File(pathString+"/"+(i+1)+".jpeg");
if (!imgFile.exists()) {
if(!imgFile.isDirectory()){
imgFile.getParentFile().mkdirs();
}
imgFile.createNewFile();
}
FileOutputStream out = new FileOutputStream(pathString+"/"+(i+1)+".jpeg");
imgList.add(proString2+(i+1)+".jpeg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
String imgcontentString = "";
//遍历列表,循环输出图片
if(imgList.size()>0){
for (String string : imgList) {
imgcontentString = imgcontentString
+ "<p valign='middle' align='center'><img src='"+string+"' width='326.95pt' height='266.95pt' /></p></br>";
}
}
System.out.println("success!!");
return imgcontentString;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
return null;
}
public static boolean doPPtoImage(File file,String projectPath){
boolean isppt = checkFile(file);
if (!isppt) {
System.out.println("The image you specify don't exit!");
return false;
}
try {
FileInputStream is = new FileInputStream(file);
HSLFSlideShow hss = new HSLFSlideShow(is);
is.close();
Dimension pgsize = hss.getPageSize();
List<HSLFSlide> slides = hss.getSlides();
for (int i = 0; i < slides.size(); i++) {
System.out.println("第"+i+"页");
List<List<HSLFTextParagraph>> sTextRun = slides.get(i).getTextParagraphs();
for (int j = 0; j < sTextRun.size(); j++) {
List<HSLFTextParagraph> htps = sTextRun.get(j);
for (int k = 0; k < htps.size(); k++) {
List<HSLFTextRun> htr = htps.get(k).getTextRuns();
for (int l = 0; l < htr.size(); l++) {
htr.get(l).setFontIndex(1);
htr.get(l).setFontFamily("宋体");
System.out.println(htr.get(l).getRawText());
}
}
}
BufferedImage img = new BufferedImage(pgsize.width, pgsize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = img.createGraphics();
graphics.setColor(java.awt.Color.BLUE);
graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));
slides.get(i).draw(graphics);
//这里设置图片的存放路径和图片的格式(jpeg,png,bmp等),注意生成文件路径
FileOutputStream out = new FileOutputStream("D:/temp/pptToImg/"+(i+1)+".jpeg");
javax.imageio.ImageIO.write(img, "jpeg", out);
out.close();
}
System.out.println("success!!");
return true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return false;
}
//function 检查文件是否为PPT
private static boolean checkFile(File file) {
// TODO Auto-generated method stub
boolean isppt = false;
String filenameString = file.getName();
String suffixnameString = null;
if(filenameString != null && filenameString.indexOf(".")!=-1){
suffixnameString = filenameString.substring(filenameString.indexOf("."));
if (suffixnameString.equals(".ppt")||suffixnameString.equals(".pptx")){
isppt = true;
}
return isppt;
}else {
return isppt;
}
}
}