合并 ppt

合并ppt (要解决格式需要购买专业的jar包,下面一种免费的格式处理不了,一种收费的可以处理格式问题)
代码(不处理格式)

package com.baic.util;

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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 合并ppt
 * 
 * @author wyh
 *
 */
public class PPTMergeUtils {
     

	private static Logger logger = LoggerFactory.getLogger(PPTMergeUtils.class);

	/**
	 * 合并连个ppt为一个
	 * 
	 * @param filePath1
	 *            第一个ppt
	 * @param filePath2
	 *            第二个ppt
	 * @param filePath3
	 *            合并后的ppt
	 */
	public static void mergePPt(String filePath1, String filePath2, String filePath3) {
     
		try {
     
			// creating empty presentation
			XMLSlideShow ppt = new XMLSlideShow();
			// taking the two presentations that are to be merged
			String[] inputs = {
      filePath1, filePath2 };
			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);
				}
			}
			FileInputStream inputstream = new FileInputStream(filePath1);
			XMLSlideShow src = new XMLSlideShow(inputstream);
			// 删除第一个文件
			new File(filePath1).delete();
			// 删除第二个文件
			new File(filePath2).delete();
			// creating the file object
			FileOutputStream out = new FileOutputStream(filePath3);
			// saving the changes to a file
			ppt.write(out);
			out.close();
		} catch (IOException e) {
     
			logger.error("合并ppt", e);
		}
	}
}

代码(处理格式)

package com.ebon.hello.controller;

import com.spire.presentation.*;

public class HeBingPPt {
     
    public static void main(String[] args) throws Exception{
     
        //加载文档1,文档2
        Presentation ppt1 = new Presentation();
        ppt1.loadFromFile("D://template//b.pptx");
        Presentation ppt2 = new Presentation();
        ppt2.loadFromFile("D://template//a.pptx");

        //遍历文档1的所有幻灯片,添加到文档2
        for(int i = 0;i<ppt1.getSlides().getCount();i++){
     
            ppt2.getSlides().append(ppt1.getSlides().get(i));
        }

        //保存文档2
        ppt2.saveToFile("D://template//c.pptx",FileFormat.PPTX_2013);
        ppt2.dispose();
    }
}

包导入(只能免费合并10张)

<repositories>
    <repository>
         <id>com.e-iceblueid>
         <name>e-icebluename>
         <url>http://repo.e-iceblue.com/nexus/content/groups/public/url>
     repository>
repositories>

<dependency>
  <groupId>e-icebluegroupId>
    <artifactId>spire.presentation.freeartifactId>
    <version>2.6.1version>
dependency>

jar包官网:(https://www.e-iceblue.cn/licensing/install-spirepdf-for-java-from-maven-repository.html)

你可能感兴趣的:(合并 ppt)