Apache POI PPT - PPT轉(zhuǎn)換為圖片

2018-12-27 15:58 更新

您可以將演示文稿轉(zhuǎn)換為圖像文件。 以下程序顯示了如何去做。

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 ,然后從命令提示符處編譯并執(zhí)行它,如下所示:

$javac PpttoPNG.java
$java PpttoPNG

它將編譯并執(zhí)行以生成以下輸出:

Image created successfully

以下快照顯示了作為輸入的演示:

下面給出了在指定位置創(chuàng)建的映像的快照。


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號