Apache POI PPT - 合并

2018-12-27 15:55 更新

您可以使用 XMLSlideShow 類的 importContent()方法合并多個(gè)演示文稿。 下面給出的是合并兩個(gè)演示文稿的完整程序:

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 MergingMultiplePresentations {
   
      public static void main(String args[]) throws IOException{
      
      //creating empty presentation
      XMLSlideShow ppt = new XMLSlideShow();
      
      //taking the two presentations that are to be merged 
      String file1 = "presentation1.pptx";
      String file2 = "presentation2.pptx";
      String[] inputs = {file1, file2};
      
      for(String arg : inputs){
      
         FileInputStream inputstream = new FileInputStream(arg);
         XMLSlideShow src = new XMLSlideShow(inputstream);
         
         for(XSLFSlide srcSlide : src.getSlides()){
         
            //merging the contents
            ppt.createSlide().importContent(srcSlide);
         }
      }
     
      String file3 = "combinedpresentation.pptx";
      
      //creating the file object
      FileOutputStream out = new FileOutputStream(file3);
      
      // saving the changes to a file
      ppt.write(out);
      System.out.println("Merging done successfully");
      out.close();
   }
}

將上述代碼保存為 MergingMultiplePresentations.java ,然后從命令提示符處編譯并執(zhí)行它,如下所示:

$javac MergingMultiplePresentations.java
$java MergingMultiplePresentations

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

Merging done successfully

以下快照顯示第一個(gè)演示:

以下快照顯示第二個(gè)演示:

下面給出合并兩個(gè)幻燈片后程序的輸出。 在這里您可以看到合并在一起的早期幻燈片的內(nèi)容。


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

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)