利用POI将PPT转换为图片
2013-08-27 13:59
281 查看
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); } }上面这个方法是竖着拼装,大家也可以改成横向或者其他方式拼装图片。
相关文章推荐
- poi 读取 ppt,并将内容转换成图片
- 利用POI抽取PPT中的图片并保存在文件中
- java转换ppt,ppt转成图片 乱码问题解决
- 利用CImage完成图片的显示、拷贝、绘制及转换图片格式
- [置顶] 利用openoffice+jodconverter-code-3.0-bate4 把ppt转图片
- 利用python编写一个图片主色转换的脚本
- Flex中利用ColorMatrixFilter将图片转换为黑白图片的例子
- java转换ppt,ppt转成图片 乱码问题解决
- 利用POI将word转换成html实现在线阅读
- 利用POI技术 处理PPT
- 利用wps的API将word、excel、ppt转换为pdf
- 利用CImage完成图片的显示、拷贝、绘制及转换图片格式 .
- java利用poi添加多张图片到word
- [置顶] Java 利用poi给word添加页眉页脚,页眉左边为公司logo图片,右为公司全称,页脚左为公司地址,右为页码总页数
- 利用POI抽取word中的图片并保存在文件中
- 利用Matlab将图片转换成素描(简笔画)风格
- python添加win32com包,将ppt转换为图片
- java-poi3.17读取ppt文本和图片
- 利用CImage完成图片的显示、拷贝、绘制及转换图片格式
- VISIO、WORD、PPT等输出文件转换成EPS图片