在Java编程中,如何合并两个PPT文档?
注意:需要访问网址:http://poi.apache.org/download.html , 下载一个Apache POI软件包。这里下载最新版本:poi-bin-3.17-20170915.tar.gz解压并全部导入 。
需要导入全部包,如下图所示 -
以下是使用Java合并两个PPT文档的程序。
package com.yiibai;
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 = "aboutus1.pptx";
String file2 = "aboutus2.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 = "aboutus12.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();
}
}
执行上面示例代码,得到以下结果 -
Merging done successfully
这就创建一个PPT文件:aboutus12.pptx了,如下所示 -