Java 合並、拆分PPT幻燈片


在日常使用PPT時,為了便於操作和管理文檔,時常會遇到需要將PPT幻燈片進行合並或拆分的情況。本文將通過Java程序來演示如何進行上述操作。

示例要點:

1. 合並PPT幻燈片

1.1 將第一個PPT文檔中的指定幻燈片數據,寫入到第二個PPT文檔的指定位置。

1.2 加載多個獨立的PPT文檔,並將第一個文檔中的所有幻燈片數據添加到第二個文檔中。

2. 拆分PPT幻燈片

2.1 按每一頁拆分
2.2 按指定幻燈片頁數范圍拆分

環境配置:

  • Intellij Idea2019.1(下載配置教程戳鏈接
  • JDK 1.8.0(下載安裝教程戳鏈接
  • Spire.Presentation.jar(通過官網下載安裝包,解壓后將lib文件夾下的Spire.Presentation.jar手動導入IDEA中。)

測試文檔如下:

【示例1】合並PPT幻燈片

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

 1 import com.spire.presentation.*;
 2 
 3 public class MergePPT1 {
 4     public static void main(String[] args) throws Exception {
 5         //加載文檔1,獲取第三張幻燈片
 6         Presentation ppt1 = new Presentation();
 7         ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");
 8         ISlide slide = ppt1.getSlides().get(2);
 9 
10         //加載文檔2,將文檔1中獲取的幻燈片作為第二張插入到文檔2
11         Presentation ppt2 = new Presentation();
12         ppt2.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample2.pptx");
13         int index = 1;
14         ppt2.getSlides().insert(index,slide);
15 
16         //保存文檔2
17         ppt2.saveToFile("output/merge1.pptx",FileFormat.PPTX_2013);
18         ppt2.dispose();
19     }
20 }

合並效果:

Part 2 將多個幻燈片文檔合並為一個文檔

 1 import com.spire.presentation.*;
 2 
 3 public class MergePPT2 {
 4     public static void main(String[] args) throws Exception {
 5         //加載文檔1,文檔2
 6         Presentation ppt1 = new Presentation();
 7         ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");
 8         Presentation ppt2 = new Presentation();
 9         ppt2.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample2.pptx");
10 
11         //遍歷文檔1的所有幻燈片,添加到文檔2
12         for(int i = 0;i<ppt1.getSlides().getCount();i++){
13             ppt2.getSlides().append(ppt1.getSlides().get(i));
14         }
15 
16         //保存文檔2
17         ppt2.saveToFile("output/merge2.pptx",FileFormat.PPTX_2013);
18         ppt2.dispose();
19     }
20 }

合並效果:

【示例2】拆分PPT幻燈片

Part 1 按幻燈片每一頁來拆

 1 import com.spire.presentation.*;
 2 public class SplitPPT1 {
 3     public static void main(String[] args) throws Exception {
 4         //加載測試文檔1
 5         Presentation ppt1 = new Presentation();
 6         ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");
 7 
 8         //遍歷文檔1
 9         for (int i = 0; i < ppt1.getSlides().getCount(); i++) {
10         
11        //新建一個PPT文檔,並移除默認生成的第一頁幻燈片
12             Presentation newppt = new Presentation();
13             newppt.getSlides().removeAt(0);
14             
15        //將每一頁添加到新建的文檔,並保存
16             newppt.getSlides().append(ppt1.getSlides().get(i));
17             newppt.saveToFile(String.format("output/單頁拆分-%1$s.pptx", i), FileFormat.PPTX_2013);
18         }
19     }
20 }

拆分效果:

Part 2 按指定幻燈片頁數范圍來拆分
 1 import com.spire.presentation.*;
 2 
 3 public class SplitPPT2 {
 4     public static void main(String[] args) throws Exception {
 5         //加載文檔1
 6         Presentation ppt1 = new Presentation();
 7         ppt1.loadFromFile("C:\\Users\\Test1\\Desktop\\Sample1.pptx");
 8 
 9         //新建文檔1,移除默認生成的第一頁幻燈片
10         Presentation newppt1 = new Presentation();
11         newppt1.getSlides().removeAt(0);
12 
13         //將文檔1中的第一、二頁添加到新建的文檔1,並保存
14         for (int i = 0; i < 2; i++)
15         {
16             newppt1.getSlides().append(ppt1.getSlides().get(i));
17         }
18         newppt1.saveToFile(String.format("output/拆分1.pptx"), FileFormat.PPTX_2013);
19 
20         //新建文檔2,移除默認生成的第一頁幻燈片
21         Presentation newppt2 = new Presentation();
22         newppt2.getSlides().removeAt(0);
23 
24         //將文檔2中的第三、四、五頁添加到新建的文檔2,並保存
25         for(int j = 2;j < 5;j++){
26             newppt2.getSlides().append(ppt1.getSlides().get(j));
27         }
28         newppt2.saveToFile(String.format("output/拆分2.pptx"), FileFormat.PPTX_2013);
29     }
30 }

拆分效果:

總結

通過以上代碼示例,我們可以將PPT文檔按照需求進行合並或拆分。若對代碼或產品包導入有疑問,可評論或私信。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM