Java 合併、拆分PPT幻燈片

隨着PPT文檔在平常工做中的使用愈來愈頻繁,爲了便於操做和管理文檔,時常會遇到須要將PPT幻燈片進行合併或拆分的狀況。通常來講,合併包括將指定幻燈片合併到文檔、將多個幻燈片文檔合併爲一個文檔;拆分包括按幻燈片每一頁單獨拆分爲一個文檔、按指定幻燈片頁數範圍來拆分爲多個文檔。本文將經過使用Java程序來演示以上四種合併和拆分PPT文檔的狀況。html

使用工具:Free Spire.Presentation for Java(免費版)

Jar文件獲取及導入:

方法1:經過官網下載獲取jar包。解壓後將lib文件夾下的Spire.Presentation.jar導入Java程序。(以下圖)
安裝圖.png
方法2:經過maven倉庫安裝導入。具體安裝教程參見此網頁java

測試文檔以下:app

測試文檔.png

【示例1】合併PPT幻燈片

Part 1 將指定幻燈片合併到文檔maven

import com.spire.presentation.*;

public class MergePPT1 {
    public static void main(String[] args) throws Exception {
        //加載文檔1,獲取第三張幻燈片
        Presentation ppt1 = new Presentation();
        ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");
        ISlide slide = ppt1.getSlides().get(2);

        //加載文檔2,將文檔1中獲取的幻燈片做爲第二張插入到文檔2
        Presentation ppt2 = new Presentation();
        ppt2.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample2.pptx");
        int index = 1;
        ppt2.getSlides().insert(index,slide);

        //保存文檔2
        ppt2.saveToFile("output/merge1.pptx",FileFormat.PPTX_2013);
        ppt2.dispose();
    }
}

合併效果:ide

效果1.png

Part 2 將多個幻燈片合併爲一個文檔工具

import com.spire.presentation.*;

public class MergePPT2 {
    public static void main(String[] args) throws Exception {
        //加載文檔1,文檔2
        Presentation ppt1 = new Presentation();
        ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");
        Presentation ppt2 = new Presentation();
        ppt2.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample2.pptx");

        //遍歷文檔1的全部幻燈片,添加到文檔2
        for(int i = 0;i<ppt1.getSlides().getCount();i++){
        ppt2.getSlides().append(ppt1.getSlides().get(i));
        }

        //保存文檔2
        ppt2.saveToFile("output/merge2.pptx",FileFormat.PPTX_2013);
        ppt2.dispose();
    }
}

合併效果:測試

效果2.png

【示例2】拆分PPT幻燈片

Part 1 按幻燈片每一頁來拆分spa

import com.spire.presentation.*;
public class SplitPPT1 {
    public static void main(String[] args) throws Exception {
        //加載測試文檔1
        Presentation ppt1 = new Presentation();
        ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");

        //遍歷文檔1
        for (int i = 0; i < ppt1.getSlides().getCount(); i++) {
        
        //新建一個PPT文檔,並移除默認生成的第一頁幻燈片
        Presentation newppt = new Presentation();
        newppt.getSlides().removeAt(0);
        
        //將每一頁添加到新建的文檔,並保存
        newppt.getSlides().append(ppt1.getSlides().get(i));
        newppt.saveToFile(String.format("output/單頁拆分-%1$s.pptx", i), FileFormat.PPTX_2013);
        }
    }
}

拆分效果:3d

效果3.png

Part 2 按指定幻燈片頁數範圍來拆分code

import com.spire.presentation.*;

public class SplitPPT2 {
    public static void main(String[] args) throws Exception {
        //加載文檔1
        Presentation ppt1 = new Presentation();
        ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");

        //新建文檔1,移除默認生成的第一頁幻燈片
        Presentation newppt1 = new Presentation();
        newppt1.getSlides().removeAt(0);

        //將文檔1中的第1、二頁添加到新建的文檔1,並保存
        for (int i = 0; i < 2; i++)
        {
            newppt1.getSlides().append(ppt1.getSlides().get(i));
        }
        newppt1.saveToFile(String.format("output/拆分1.pptx"), FileFormat.PPTX_2013);

        //新建文檔2,移除默認生成的第一頁幻燈片
        Presentation newppt2 = new Presentation();
        newppt2.getSlides().removeAt(0);

        //將文檔2中的第3、4、五頁添加到新建的文檔2,並保存
        for(int j = 2;j < 5;j++){
            newppt2.getSlides().append(ppt1.getSlides().get(j));
        }
        newppt2.saveToFile(String.format("output/拆分2.pptx"), FileFormat.PPTX_2013);
    }
}

拆分效果:

效果4.png

(本文完)

相關文章
相關標籤/搜索