Apache POI PPT - PPT到图片

  • 简述

    您可以将演示文稿转换为图像文件。 下面的程序展示了如何去做。
    
    
    import java.awt.Color;
    
    import java.awt.Dimension;
    
    import java.awt.Graphics2D;
    
    import java.awt.geom.Rectangle2D;
    
    import java.awt.image.BufferedImage;
    
    import java.io.File;
    
    import java.io.FileInputStream;
    
    import java.io.FileOutputStream;
    
    import java.io.IOException;
    
    import java.util.List;
    
    import org.apache.poi.xslf.usermodel.XMLSlideShow;
    
    import org.apache.poi.xslf.usermodel.XSLFSlide;
    
    
    
    public class PptToImage {
    
       public static void main(String args[]) throws IOException {
    
          //creating an empty presentation
    
          File file=new File("pptToImage.pptx");
    
          XMLSlideShow ppt = new XMLSlideShow(new FileInputStream(file));
    
          
    
          //getting the dimensions and size of the slide 
    
          Dimension pgsize = ppt.getPageSize();
    
          List<XSLFSlide> slide = ppt.getSlides();
    
          BufferedImage img = null;
    
          for (int i = 0; i < slide.size(); i++) {
    
             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.get(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
    
    
    以下快照显示了作为输入给出的演示文稿 -
    presentation1
    下面给出的是在指定位置创建的图像的快照。
    PPT Image