第一次学使用软件去读取Office类文件。
今天百度了一下,发现Apache也是有这样的开源项目的,POI提供下列支持:
测试了一下HSLF对PPT的读操作,并将PPT转化为图片保存到指定目录:
使用之前必须将包引入:
关联包:
测试主函数如下:
设置输入ppt文件、输出路径、以及生成图片规模大小。
1 package edu.office; 2 3 import java.util.List; 4 5 public class TestTransfor { 6 7 /** 8 * 测试ppt转换 9 * 10 * **/ 11 public static void main(String[] args) { 12 PPTReader reader = new PPTReader(); 13 reader.setPPT_file_path("D:/1.ppt"); 14 reader.setOutput_path("D:/ppt/"); 15 //控制图片尺寸 1倍 2倍 3倍 16 reader.setScale(1); 17 18 List<String> images = reader.ppt2png(); 19 for(String image : images){ 20 System.out.println(image); 21 } 22 } 23 24 }
PPT转化png类如下:
1 package edu.office; 2 3 import java.awt.Color; 4 import java.awt.Dimension; 5 import java.awt.Graphics2D; 6 import java.awt.RenderingHints; 7 import java.awt.geom.Rectangle2D; 8 import java.awt.image.BufferedImage; 9 import java.io.File; 10 import java.io.FileInputStream; 11 import java.io.FileNotFoundException; 12 import java.io.FileOutputStream; 13 import java.io.IOException; 14 import java.util.ArrayList; 15 import java.util.List; 16 17 import javax.imageio.ImageIO; 18 19 import org.apache.poi.hslf.model.Slide; 20 import org.apache.poi.hslf.usermodel.SlideShow; 21 22 public class PPTReader{ 23 24 //ppt输入文件路径 25 private String ppt_file_path; 26 //ppt2png保存png图片的路径 27 private String output_path; 28 //scale是图片是原来的size几倍 29 private int scale = 1; 30 31 public String getPpt_file_path() { 32 return ppt_file_path; 33 } 34 public void setPPT_file_path(String ppt_file_path) { 35 this.ppt_file_path = ppt_file_path; 36 } 37 public int getScale() { 38 return scale; 39 } 40 public void setScale(int scale) { 41 this.scale = scale; 42 } 43 public String getOutput_path() { 44 return output_path; 45 } 46 public void setOutput_path(String output_path) { 47 this.output_path = output_path; 48 } 49 50 /**ppt转png 51 * 52 * @return null 失败 list<String> 图片名称列表 53 * **/ 54 public List<String> ppt2png(){ 55 //int slidenum = 2; 56 List<String> images = new ArrayList<String>(); 57 try { 58 FileInputStream is; 59 is = new FileInputStream(ppt_file_path); 60 SlideShow ppt = new SlideShow(is); 61 is.close(); 62 63 Dimension pgsize = ppt.getPageSize(); 64 65 int width = (int)(pgsize.width*scale); 66 int height = (int)(pgsize.height*scale); 67 68 Slide[] slide = ppt.getSlides(); 69 for (int i = 0; i < slide.length; i++) { 70 71 //if (slidenum != -1 && slidenum != (i+1)) continue; 72 //获取每页ppt标题 73 String title = slide[i].getTitle(); 74 //System.out.println("Rendering slide "+slide[i].getSlideNumber() + (title == null ? "" : ": " + title)); 75 76 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); 77 Graphics2D graphics = img.createGraphics(); 78 graphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); 79 graphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); 80 graphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); 81 graphics.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); 82 83 graphics.setPaint(Color.white); 84 graphics.fill(new Rectangle2D.Float(0, 0, width, height)); 85 86 graphics.scale((double)width/pgsize.width, (double)height/pgsize.height); 87 88 slide[i].draw(graphics); 89 //对每一张图片进行命名 90 String fname = output_path + "ppt-" + (i+1) + ".png"; 91 images.add("ppt-" + (i+1) + ".png"); 92 File file = new File(output_path); 93 if(!file.exists()){ 94 file.mkdirs(); 95 } 96 FileOutputStream out = new FileOutputStream(fname); 97 ImageIO.write(img, "png", out); 98 out.close(); 99 } 100 return images; 101 } catch (FileNotFoundException e) { 102 e.printStackTrace(); 103 } catch (IOException e) { 104 e.printStackTrace(); 105 } 106 return null; 107 } 108 }