POI 除了可以操作word、excel之外,还可以操作ppt文档,不过我没找到如何用POI将ppt文档转换为html,目前只找到ppt转图片的方法,该方法有许多地方需要改进,比如图片失真较严重,背景颜色无法显示等。
public static void convertPPT2Image(String pptFilePath,String imageFilePath)
throws IOException
{
File pptFile = new File(pptFilePath);
File imageFile = new File(imageFilePath);
File imageFileParent = new File(imageFile.getParent());
List<File> multiImageFiles = new ArrayList<File>();//因为一张幻灯片生成一张图片,需要将所有的图片保存起来,供后面拼装成一张图片
InputStream is = null;
OutputStream out = null;
try{
if(pptFile.exists()){
if(!imageFileParent.exists()){
imageFileParent.mkdirs();
}
is = new FileInputStream(pptFile);
SlideShow ppt = new SlideShow(is);
Dimension pgSize = ppt.getPageSize();
Slide[] slide = ppt.getSlides();
for(int i = 0 ; i < slide.length; i++){
TextRun[] textRuns = slide[i].getTextRuns();
for(int k = 0; k < textRuns.length; k++){
RichTextRun[] richTextRuns = textRuns[k].getRichTextRuns();
for(int j = 0; j < richTextRuns.length; j++){
richTextRuns[j].setFontIndex(1);
richTextRuns[j].setFontName("宋体");
}
}
BufferedImage image = new BufferedImage(pgSize.width,pgSize.height,BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = image.createGraphics();
graphics.fill(new Rectangle2D.Float(0,0,pgSize.width,pgSize.height));
slide[i].draw(graphics);
String tempFileName = imageFile.getParent()+"/"+i+imageFile.getName();
out = new FileOutputStream(tempFileName);
multiImageFiles.add(new File(tempFileName));
ImageIO.write(image, "jpg", out);
out.close();
is.close();
}
mergeMultiImageFiles(multiImageFiles,imageFile);//该方法将多个图片拼装为一张图片
}
}finally{
try{
if(is != null){
is.close();
}
if(out != null){
out.close();
}
}catch(IOException e){
e.printStackTrace();
}
}
}
public static void mergeMultiImageFiles(List<File> imageFiles,File image)
throws IOException
{
if(imageFiles != null && imageFiles.size() > 0){
BufferedImage imageNew = null;
for(int i = 0 ; i < imageFiles.size(); i++){
BufferedImage imageBuffer = ImageIO.read(imageFiles.get(i));
int width = imageBuffer.getWidth();
int height = imageBuffer.getHeight();
if(imageNew == null){
imageNew = new BufferedImage(width,(height + SEPARATE_DISTANCE)* imageFiles.size(),BufferedImage.TYPE_INT_RGB);
}
int[] imageRgbArray = new int[width * height];
imageRgbArray = imageBuffer.getRGB(0, 0, width, height, imageRgbArray, 0, width);
imageNew.setRGB(0, (height+SEPARATE_DISTANCE) * i, width, height, imageRgbArray, 0, width);//SEPARATE_DISTANCE表示两张图片的间隔距离
}
ImageIO.write(imageNew, "jpg", image);
}
}
上面这个方法是竖着拼装,大家也可以改成横向或者其他方式拼装图片。