JAVA+POI实现PPT的在线预览
接前几篇文章,实现PPT的在线预览,过程如下:
1)将PPT转为一个图片集合保存在一个文件夹中
2)将图片集合转化为PDF,从而实现PPT的预览
在实现这一功能的过程中,参阅了很多博客和资料,才算是完整的实现这一功能,但是 PPT转化为图片的时候依然会有些许问题,比如:PPT中 的表格会缺失,这一问题还未得到解决,之后若是实现的话,会继续补充的。
看了好多文章,好像就是只能 两步转实现这个功能,未查到PPT直接预览或者PPT直接转为PDF格式的,如果大家有更好的解决方法的话,如若愿意分享的,还请留言。万分感谢!!!
此文章只是为了记录自己学习的过程,经测有效~~~
PPT转为图片集合实现类:
package cn.itcast.QLN.utils;
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.imageio.ImageIO;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.hslf.extractor.PowerPointExtractor;
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.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.xslf.extractor.XSLFPowerPointExtractor;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.drawingml.x2006.main.CTRegularTextRun;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextBody;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextFont;
import org.openxmlformats.schemas.drawingml.x2006.main.CTTextParagraph;
import org.openxmlformats.schemas.presentationml.x2006.main.CTGroupShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTShape;
import org.openxmlformats.schemas.presentationml.x2006.main.CTSlide;
/**
* PPT转image工具类
* 使用的Apache poi-3.14的版本 依赖第三方jar包:poi-3.14-20160307.jar、poi-ooxml-3.14-20160307.jar、
* poi-ooxml-schemas-3.14-20160307.jar、poi-scratchpad-3.14-20160307.jar、xmlbeans-2.6.0.jar
* @author yds
* @date 2017-03-22
*
*/
public class ppt2img {
/**
* 将PPTX 文件转换成image
* @param orignalPPTFileName //PPTX文件路径 如:d:/demo/demo1.pptx
* @param targetImageFileDir //转换后的图片保存路径 如:d:/demo/pptxImg
* @param imageFormatNameString //图片转化的格式字符串 ,如:"jpg"、"jpeg"、"bmp" "png" "gif" "tiff"
* @return Map
* key: converReturnResult 类型:boolean 转化结果 true 代表转换成功,false 代表转换失败
* key:imgNames 类型:List 转换成功后图片的全部名称集合
* 注:获取“imgNames”图片名称集合时,请先判断“converReturnResult” 是否为true;如果有一张转换失败则为false
*/
@SuppressWarnings("resource")
public static Map converPPTXtoImage(String orignalPPTFileName,String targetImageFileDir,
String imageFormatNameString){
Map map=new HashMap();
boolean converReturnResult=true;//是否全部转成功
List imgNamesList=new ArrayList();//PPT转成图片后所有名称集合
FileInputStream orignalPPTFileInputStream=null;
FileOutputStream orignalPPTFileOutStream=null;
XMLSlideShow oneSlideShow=null;
try{
try {
orignalPPTFileInputStream=new FileInputStream(orignalPPTFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
try {
oneSlideShow=new XMLSlideShow(orignalPPTFileInputStream);
} catch (IOException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
//获取PPT每页的尺寸大小(宽和高度)
Dimension onePPTPageSize=oneSlideShow.getPageSize();
//获取PPT文件中的所有PPT页面,并转换为一张张播放片
List pptPageXSLFSLiseList= oneSlideShow.getSlides();
String xmlFontFormat=""+
""+
"";
for (int i = 0; i < pptPageXSLFSLiseList.size(); i++) {
/**
* 设置中文为宋体,解决中文乱码问题
*/
CTSlide oneCTSlide=pptPageXSLFSLiseList.get(i).getXmlObject();
CTGroupShape oneCTGroupShape=oneCTSlide.getCSld().getSpTree();
List oneCTShapeList=oneCTGroupShape.getSpList();
for (CTShape ctShape : oneCTShapeList) {
CTTextBody oneCTTextBody = ctShape.getTxBody();
if(null==oneCTTextBody){
continue;
}
CTTextParagraph[] oneCTTextParagraph= oneCTTextBody.getPArray();
CTTextFont oneCTTextFont=null;
try {
oneCTTextFont=CTTextFont.Factory.parse(xmlFontFormat);
} catch (XmlException e) {
e.printStackTrace();
}
for (CTTextParagraph ctTextParagraph : oneCTTextParagraph) {
CTRegularTextRun[] onrCTRegularTextRunArray=ctTextParagraph.getRArray();
for (CTRegularTextRun ctRegularTextRun : onrCTRegularTextRunArray) {
CTTextCharacterProperties oneCTTextCharacterProperties =ctRegularTextRun.getRPr();
oneCTTextCharacterProperties.setLatin(oneCTTextFont);
}
}
}
//创建BufferedImage 对象,图像尺寸为原来的PPT的每页尺寸
BufferedImage oneBufferedImage=new BufferedImage(onePPTPageSize.width, onePPTPageSize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D oneGraphics2D = oneBufferedImage.createGraphics();
//将PPT文件中的每个页面中的相关内容画到转换后的图片中
pptPageXSLFSLiseList.get(i).draw(oneGraphics2D);
/**
* 设置图片的存放路径和图片格式,注意生成的文件路径为绝对路径,最终获得各个图像文件所对应的输出流的对象
*/
try {
String imgName=(i+1)+"_"+UUID.randomUUID().toString()+"."+imageFormatNameString;
imgNamesList.add(imgName);//将图片名称添加的集合中
orignalPPTFileOutStream=new FileOutputStream(targetImageFileDir+imgName);
} catch (FileNotFoundException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
//将转换后的各个图片文件保存带指定的目录中
try {
ImageIO.write(oneBufferedImage, imageFormatNameString, orignalPPTFileOutStream);
} catch (IOException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
}
} finally{
try {
if(orignalPPTFileInputStream!=null){
orignalPPTFileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(orignalPPTFileOutStream!=null){
orignalPPTFileOutStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
map.put("converReturnResult", converReturnResult);
map.put("imgNames", imgNamesList);
}
return map;
}
/**
* 将PPT 文件转换成image
* @param orignalPPTFileName //PPT文件路径 如:d:/demo/demo1.ppt
* @param targetImageFileDir //转换后的图片保存路径 如:d:/demo/pptImg
* @param imageFormatNameString //图片转化的格式字符串 ,如:"jpg"、"jpeg"、"bmp" "png" "gif" "tiff"
* @return Map
* key: converReturnResult 类型:boolean 转化结果 true 代表转换成功,false 代表转换失败
* key:imgNames 类型:List 转换成功后图片的全部名称集合
* 注:获取“imgNames”图片名称集合时,请先判断“converReturnResult” 是否为true;如果有一张转换失败则为false
*/
@SuppressWarnings("resource")
public static Map converPPTtoImage(String orignalPPTFileName,String targetImageFileDir,
String imageFormatNameString){
Map map=new HashMap();
boolean converReturnResult=true;//是否全部转成功
List imgNamesList=new ArrayList();//PPT转成图片后所有名称集合
FileInputStream orignalPPTFileInputStream=null;
FileOutputStream orignalPPTFileOutStream=null;
HSLFSlideShow oneHSLFSlideShow=null;
try{
try {
orignalPPTFileInputStream=new FileInputStream(orignalPPTFileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
try {
oneHSLFSlideShow=new HSLFSlideShow(orignalPPTFileInputStream);
} catch (IOException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
//获取PPT每页的大小(宽和高度)
Dimension onePPTPageSize= oneHSLFSlideShow.getPageSize();
//获得PPT文件中的所有的PPT页面(获得每一张幻灯片),并转为一张张的播放片
List pptPageSlideList=oneHSLFSlideShow.getSlides();
//下面循环的主要功能是实现对PPT文件中的每一张幻灯片进行转换和操作
for (int i = 0; i
//这几个循环只要是设置字体为宋体,防止中文乱码,
List> oneTextParagraphs=pptPageSlideList.get(i).getTextParagraphs();
for (List list :oneTextParagraphs) {
for (HSLFTextParagraph hslfTextParagraph : list) {
List HSLFTextRunList= hslfTextParagraph.getTextRuns();
for (int j = 0; j
/*
* 如果PPT在WPS中保存过,则 HSLFTextRunList.get(j).getFontSize();的值为0或者26040,
* 因此首先识别当前文本框内的字体尺寸是否为0或者大于26040,则设置默认的字体尺寸。
*
*/
//设置字体大小
Double size= HSLFTextRunList.get(j).getFontSize();
if((size<=0)||(size>=26040)){
HSLFTextRunList.get(j).setFontSize(7.0);
}
//设置字体样式为宋体
// String family=HSLFTextRunList.get(j).getFontFamily();
HSLFTextRunList.get(j).setFontFamily("宋体");
}
}
}
/**
* 创建BufferedImage对象,图像的尺寸为原来的每页的尺寸
*/
BufferedImage oneBufferedImage=new BufferedImage(onePPTPageSize.width, onePPTPageSize.height, BufferedImage.TYPE_INT_RGB);
Graphics2D oneGraphics2D=oneBufferedImage.createGraphics();
/**
* 设置转换后的图片背景色为白色
*
*/
oneGraphics2D.setPaint(Color.white);
oneGraphics2D.fill(new Rectangle2D.Float(0,0,onePPTPageSize.width,onePPTPageSize.height));
pptPageSlideList.get(i).draw(oneGraphics2D);
/**
* 设置图片的存放路径和图片格式,注意生成的图片路径为绝对路径,最终获得各个图像文件所对应的输出流对象
*/
try {
String imgName=(i+1)+"_"+UUID.randomUUID().toString()+"."+imageFormatNameString;
imgNamesList.add(imgName);//将图片名称添加的集合中
orignalPPTFileOutStream=new FileOutputStream(targetImageFileDir+imgName);
} catch (FileNotFoundException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
/**
* 转换后的图片文件保存的指定的目录中
*/
try {
ImageIO.write(oneBufferedImage, imageFormatNameString, orignalPPTFileOutStream);
// throw new IOException();
} catch (IOException e) {
e.printStackTrace();
converReturnResult=false;
map.put("converReturnResult", converReturnResult);
return map;
}
}
}finally{
try {
if(orignalPPTFileInputStream!=null){
orignalPPTFileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(orignalPPTFileOutStream!=null){
orignalPPTFileOutStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
map.put("converReturnResult", converReturnResult);
map.put("imgNames", imgNamesList);
}
return map;
}
/**
* 直接抽取幻灯片的全部内容
* @param filePath
* @return
* @throws IOException
*/
/* @SuppressWarnings("resource")
public static String readppt2003Text(String filePath) {
PowerPointExtractor extractor=null;;
try {
File file = new File(filePath);
FileInputStream fin=new FileInputStream(file);
extractor = new PowerPointExtractor(fin);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return extractor.getText();
}
*/
// 读取pptx
@SuppressWarnings("resource")
public static String readPPTX2007Text(String file) {
try {
return new XSLFPowerPointExtractor(POIXMLDocument.openPackage(file)).getText();
} catch (XmlException e) {
e.printStackTrace();
} catch (OpenXML4JException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
图片集合转PDF格式实现类
package cn.itcast.QLN.utils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.PdfWriter;
/**
* 将多张图片合并转为PDF;需要用到iTextpdf包,
*
*
*/
public class imgs2pdf {
/**
*
* @param imageFolderPath
* 图片文件夹地址
* @param pdfPath
* PDF文件保存地址
*
*/
public static void toPdf(String imageFolderPath, String pdfPath) {
try {
// 图片文件夹地址
// String imageFolderPath = "D:/Demo/ceshi/";
// 图片地址
String imagePath = null;
// PDF文件保存地址
// String pdfPath = "D:/Demo/ceshi/hebing.pdf";
// 输入流
FileOutputStream fos = new FileOutputStream(pdfPath);
// 创建文档
Document doc = new Document(null, 0, 0, 0, 0);
//doc.open();
// 写入PDF文档
PdfWriter.getInstance(doc, fos);
// 读取图片流
BufferedImage img = null;
// 实例化图片
Image image = null;
// 获取图片文件夹对象
File file = new File(imageFolderPath);
File[] files = file.listFiles();
// 循环获取图片文件夹内的图片
for (File file1 : files) {
if (file1.getName().endsWith(".png")
|| file1.getName().endsWith(".jpg")
|| file1.getName().endsWith(".gif")
|| file1.getName().endsWith(".jpeg")
|| file1.getName().endsWith(".gif")) {
// System.out.println(file1.getName());
imagePath = imageFolderPath + file1.getName();
//System.out.println(file1.getName());
// 读取图片流
img = ImageIO.read(new File(imagePath));
// 根据图片大小设置文档大小
doc.setPageSize(new Rectangle(img.getWidth(), img
.getHeight()));
// 实例化图片
image = Image.getInstance(imagePath);
// 添加图片到文档
doc.open();
doc.add(image);
}
}
// 关闭文档
doc.close();
} catch (IOException e) {
e.printStackTrace();
} catch (BadElementException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
action方法调用
if (suffix1.equals("pptx")) {
String path2 =path.substring(0, 91);
System.out.println("path2为" + path2);
//创建一个新的文件夹,并命名,每次PPT转化为图片集合的时候,
//便新创建一个文件夹,避免所有的图片都在一个图片集合中
String filefName=UUID.randomUUID().toString().replace("-", "1");
File dir = new File(path2+"\\"+filefName);
dir.mkdirs();
String path3=path2+filefName+"\\";
//System.out.println("path3为" + path3);
//PPT转化为图片集合
Map map=ppt2img.converPPTXtoImage(path,path3,"jpg");//.readPPTX2007Text(path);//("E:\\ppt\\PMS2.0界面设计培训-20130507.pptx");
boolean converReturnResult=(Boolean) map.get("converReturnResult");
System.out.println("converReturnResult为"+converReturnResult);
if(converReturnResult){//如果全部转换成功,则为true;如果有一张转换失败,则为fasle
@SuppressWarnings("unchecked")
//List imgNames中 存储的只有图片的名字
List imgNames=(List) map.get("imgNames");
/*
for (String imgName : imgNames) {
System.out.println("imgName为"+imgName);
String path4 =path3+imgName;
System.out.println("path4"+path4);
} */
//图片集合转化为pdf格式
imgs2pdf.toPdf(path3, path2+"new.pdf");
File files = new File(path2+"new.pdf");
long fileSizes = files.length();
String fileNames = files.getName();
String suffix = fileNames.substring(fileNames.lastIndexOf(".") + 1);
// 用输入流读取文件
inputStream = new FileInputStream(files);
HttpServletResponse response = ServletActionContext.getResponse();
response.setContentType(UtilTools.returnContentType(suffix));
response.setHeader("Content-Disposition", "filename=\"" + uploadFileName + ".pdf" + "\"");
byte[] buffer = new byte[(int) fileSizes];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = inputStream.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
// 确保所有数据均被读取
if (offset != buffer.length) {
throw new IOException("Could not completely read file " + files.getName());
}
response.getOutputStream().write(buffer);
response.getOutputStream().flush();
response.flushBuffer();
response.getOutputStream().close();
}
}