codecamp

Apache POI PPT - PPT转换为图片

您可以将演示文稿转换为图像文件。 以下程序显示了如何去做。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFSlide;

public class PpttoPNG {
   
   public static void main(String args[]) throws IOException{
      
      //creating an empty presentation
      File file=new File("addingimage.pptx");
      XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
      
      //getting the dimensions and size of the slide 
      Dimension pgsize = ppt.getPageSize();
      XSLFSlide[] slide = ppt.getSlides();
      
      for (int i = 0; i < slide.length; i++) {
         BufferedImage img = new BufferedImage(pgsize.width, pgsize.height,BufferedImage.TYPE_INT_RGB);
         Graphics2D graphics = img.createGraphics();

         //clear the drawing area
         graphics.setPaint(Color.white);
         graphics.fill(new Rectangle2D.Float(0, 0, pgsize.width, pgsize.height));

         //render
         slide[i].draw(graphics);
      }
      
      //creating an image file as output
      FileOutputStream out = new FileOutputStream("ppt_image.png");
      javax.imageio.ImageIO.write(img, "png", out);
      ppt.write(out);
      
      System.out.println("Image successfully created");
      out.close();	
   }
}

将上述Java代码另存为 PpttoPNG.java ,然后从命令提示符处编译并执行它,如下所示:

$javac PpttoPNG.java
$java PpttoPNG

它将编译并执行以生成以下输出:

Image created successfully

以下快照显示了作为输入的演示:

下面给出了在指定位置创建的映像的快照。


Apache POI PPT - 合并
Apache POI PPT - 快速指南
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }