![在 C# 中合併 PowerPoint 文件](images/Merge-PowerPoint-files.jpg#center)
合併 PowerPoint 演示文稿可用於各種場景,例如合併來自多個 PPT/PPTX 的內容、合併由兩個或更多人創建的單個演示文稿的部分等。在處理時,手動複製/粘貼內容的方式可能不合適與一些演示文稿。因此,本文讓 .NET 開發人員了解如何使用 C# 以編程方式合併 PowerPoint 演示文稿。
- .NET PowerPoint 合併 API
- 使用 C# 合併 PowerPoint 演示文稿
- 使用 C# 合併 PowerPoint 演示文稿的特定幻燈片
- 合併 PowerPoint 演示文稿時使用幻燈片母版
.NET PowerPoint 合併 API
Aspose.Slides for .NET 是一個功能豐富的 .NET PowerPoint API,可讓您創建和操作演示文檔。除此之外,它還允許您使用 C# 或 VB.NET 組合兩個或多個 PowerPoint 演示文稿。您可以下載 API 的 DLL 或使用 NuGet 安裝它。
使用 C# 合併 PowerPoint 演示文稿
在本節中,您將學習如何將一個 PowerPoint 演示文稿中的所有幻燈片複製和合併到另一個。為此,您可以簡單地從源演示文稿中復制幻燈片並將它們添加到目標演示文稿的末尾。以下是合併兩個演示文稿的步驟。
- 創建一個 Presentation 類的對象來加載目標演示文稿文件。
- 創建另一個 Presentation 對像以從您要克隆幻燈片的位置加載源演示文稿。
- 使用 Presentation.Slides 屬性訪問源演示文稿中的幻燈片集合。
- 循環遍歷幻燈片集合併使用 Presentation.Slides.AddClone(ISlide) 方法將它們合併到目標演示文稿中。
- 使用 Presentation.Save(String, SaveFormat) 方法保存合併的演示文稿。
以下代碼示例演示如何使用 C# 合併兩個 PowerPoint 演示文稿。
// 實例化表示目標演示文稿文件的 Presentation 對象
using (Presentation presentation1 = new Presentation("presentation1.pptx"))
{
// 實例化表示源演示文稿文件的 Presentation 對象
using (Presentation presentation2 = new Presentation("presentation2.pptx"))
{
foreach (ISlide slide in presentation2.Slides)
{
// 將幻燈片從源合併到目標
presentation1.Slides.AddClone(slide);
}
}
// 保存演示文稿
presentation1.Save("merged-presentation.pptx", Export.SaveFormat.Pptx);
}
目標演示
![目標 Powerpoint 演示文稿](images/merge-presentation1.jpg#center)
來源介紹
![要合併的源 PowerPoint 演示文稿](images/merge-presentation2.jpg#center)
合併演示
![合併 powerpoint 演示文稿 C#](images/merged-presentation.jpg#center)
使用 C# 合併 PowerPoint 演示文稿的特定幻燈片
在前面的示例中,您已將源 PPTX 文件中的所有幻燈片合併到目標 PPTX 中。但是,有時您可能只需要合併選定的幻燈片。在這種情況下,您可以使用幻燈片的索引指定要合併的幻燈片。以下是執行此操作的步驟。
- 使用 Presentation 類加載目標演示文稿。
- 使用 Presentation 類加載源演示文稿。
- 使用 presentation1.Slides.AddClone(presentation2.Slides[int Index]) 方法克隆所需的幻燈片。
- 將合併的演示文稿另存為新的 PPTX 文件。
以下代碼示例顯示如何使用 C# 合併特定的演示文稿幻燈片。
// 實例化表示目標演示文稿文件的 Presentation 對象
using (Presentation presentation1 = new Presentation("presentation1.pptx"))
{
// 實例化表示源演示文稿文件的 Presentation 對象
using (Presentation presentation2 = new Presentation("presentation2.pptx"))
{
// 僅合併 presentation2 的偶數幻燈片(第一張幻燈片位於 0 索引處)
for (int i = 1; i <= presentation2.Slides.Count; i = i + 2)
{
presentation1.Slides.AddClone(presentation2.Slides[i]);
}
}
presentation1.Save("merged-presentation-even.pptx", Export.SaveFormat.Pptx);
}
合併演示
![使用 C# 合併 powerpoint 演示文稿](images/merged-presentation-selected-slides.jpg#center)
合併 PowerPoint 演示文稿時使用幻燈片母版
在前面的兩個示例中,您合併了幻燈片,保留了源演示文稿的設計和模板。但是,在某些情況下,您可能需要根據目標演示文稿修改幻燈片的佈局。在這種情況下,您可以使用重載的 [presentation1.Slides.AddClone(presentation2.Slides1, presentation1.Masters[0], true)]12 方法。
以下代碼示例顯示如何使用 C# 中的幻燈片母版合併 PowerPoint 演示文稿中的幻燈片。
// 實例化表示目標演示文稿文件的 Presentation 對象
using (Presentation presentation1 = new Presentation("presentation1.pptx"))
{
// 實例化表示源演示文稿文件的 Presentation 對象
using (Presentation presentation2 = new Presentation("presentation2.pptx"))
{
// 僅使用幻燈片母版合併前兩張幻燈片
presentation1.Slides.AddClone(presentation2.Slides[0], presentation1.Masters[0], true);
presentation1.Slides.AddClone(presentation2.Slides[1], presentation1.Masters[0], true);
}
presentation1.Save("merged-presentation-master.pptx", Export.SaveFormat.Pptx);
}
合併演示
![使用 C# 組合 powerpoint 演示文稿](images/merged-presentation-slide-master.jpg#center)
結論
在本文中,您學習瞭如何使用 C# 合併兩個或多個 PowerPoint 演示文稿。您可以移植 C# 代碼示例,以便使用 VB.NET 合併演示文稿。如果您想探索更多關於 Aspose 的 .NET PowerPoint API,您可以訪問 文檔。
也可以看看
提示:您可能有興趣嘗試使用 Aspose 的 API 開發的用於演示的網絡查看器應用程序。