如何使用 C# 管理 PowerPoint 幻燈片 | IronPPT

如何在 PowerPoint 中使用 C# 管理幻燈片

This article was translated from English: Does it need improvement?
Translated
View the article in English

<! -- 引言實作示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

要使用 C# 管理 PowerPoint 中的幻燈片,請使用 IronPPT 的方法,例如 AddSlide() 來建立幻燈片,Remove() 來刪除幻燈片,以及 Slides 集合來以程式化的方式重新排列或隱藏幻燈片。 IronPPT文件為所有幻燈片管理作業提供全面的指南。

幻燈片是簡報中的單一頁面,是組織和顯示內容的基本構件。 幻燈片透過文字、圖片、圖表、視訊、音訊、動畫和其他設計元素直觀地傳達資訊。 在商業應用程式中,程式化的投影片管理可讓報告生成、動態簡報,以及原本需要手動編輯 PowerPoint 的重複性工作自動化。

快速入門:使用 IronPPT 輕鬆刪除、重新排序或隱藏投影片

以下是一個單行範例,顯示如何在新增第一張幻燈片後將其移除。 IronPPT 讓管理幻燈片等常見動作簡單直接,讓您專注於內容而非工具。 在生產中使用 IronPPT 之前,請確保您已設定您的授權金鑰,以避免水印。

Nuget Icon立即開始使用 NuGet 建立 PDF 檔案:

  1. 使用 NuGet 套件管理器安裝 IronPPT

    PM > Install-Package IronPPT

  2. 複製並運行這段程式碼。

    new PresentationDocument().AddSlide().Slides[0].Remove();
  3. 部署到您的生產環境進行測試

    立即開始在您的專案中使用 IronPPT,免費試用!
    arrow pointer

如何在 PowerPoint 簡報中加入幻燈片?

<! -- 說明新增幻燈片實作的圖表 --> <!--說明:說明程式碼概念的圖表或截圖 -->

使用 AddSlide 方法在簡報中新增幻燈片。 新的投影片會追加到目前投影片清單的末尾,讓簡報可以無縫擴充。 此基本操作以程式化方式建立簡報,無論是建立簡單的報表或複雜的多頁幻燈片。 如需基本範例,請參閱 製作空簡報指南

簡報中新增的幻燈片在哪裡?

使用 AddSlide() 時,新的投影片會自動追加到投影片集的末尾,以維持順序。 此預設行為可確保可預期的幻燈片定位,並簡化簡報的建構。 基於零的索引系統表示您的第一張幻燈片在索引 0 處,第二張在索引 1 處,如此類推。 在引用特定的幻燈片進行修改或移除時,瞭解這種索引方式至關重要。

我可以一次新增多張幻燈片嗎?

鏈結多個 AddSlide() 呼叫,或使用循環在單一操作中有效率地新增多個幻燈片。 這種方法在從資料庫或 API 等資料來源產生簡報時非常有效,因為這些資料來源的幻燈片數量各不相同。 考慮實施批次作業,以便在使用大量幻燈片時獲得更好的效能。

:path=/static-assets/ppt/content-code-examples/how-to/manage-slide-add-slide.cs
// Ensure you have the necessary using directives for any external libraries or namespaces.
using IronPPT;

// Instantiate a new PresentationDocument object.
var document = new PresentationDocument();

// Add three slides to the presentation.
// The AddSlide method creates a new slide and adds it to the list of slides in the document.
document.AddSlide();  // Add first slide
document.AddSlide();  // Add second slide
document.AddSlide();  // Add third slide

// Save the presentation to a file named "addSlides.pptx".
// The Save method takes a file path as an argument and writes the current state of the presentation to this file.
document.Save("addSlides.pptx");
' Ensure you have the necessary using directives for any external libraries or namespaces.
Imports IronPPT

' Instantiate a new PresentationDocument object.
Private document = New PresentationDocument()

' Add three slides to the presentation.
' The AddSlide method creates a new slide and adds it to the list of slides in the document.
document.AddSlide() ' Add first slide
document.AddSlide() ' Add second slide
document.AddSlide() ' Add third slide

' Save the presentation to a file named "addSlides.pptx".
' The Save method takes a file path as an argument and writes the current state of the presentation to this file.
document.Save("addSlides.pptx")
$vbLabelText   $csharpLabel

如何從簡報中移除幻燈片?

<! -- 說明移除幻燈片實作的示意圖 --> <!--說明:說明程式碼概念的圖表或截圖 -->

使用Remove方法刪除不需要的投影片。 此功能可讓您精煉內容並移除不必要的幻燈片,而不會打亂整體結構。 幻燈片移除對於動態簡報的產生非常重要,在動態簡報中,內容需要根據業務規則或使用者偏好有條件地包含或排除。 刪除過程會立即進行,且在程式上不可逆轉,因此請在刪除前進行驗證。

所有投影片索引位置均採用從零開始的索引方式。

移除滑動索引後會發生什麼事?

移除幻燈片時,所有後續的幻燈片都會自動上移,其索引也會重新計算,以維持連續的順序。 在迴圈中移除多張幻燈片時,這種自動重新索引的功能至關重要。移除多張投影片時,請務必在集合中向後遍歷,以避免索引移位問題,導致跳過投影片或超出範圍的異常。 對於複雜的簡報修改,請考慮以獨特的識別碼而非僅依賴索引位置來追蹤幻燈片。

如何安全無誤地移除幻燈片?

移除前檢查 Slides 計數,以防止索引超出範圍的錯誤,尤其是以程式化方式移除多張幻燈片時。 實施防禦性程式設計實務,包括邊界檢查和異常處理。 考慮建立實用方法,以驗證和錯誤報告來封裝安全移除邏輯。 在表達結構各異的生產環境中,這種方法尤其重要。

:path=/static-assets/ppt/content-code-examples/how-to/manage-slide-remove-slide.cs
// Import the IronPPT namespace to handle PowerPoint presentations
// Assuming IronPPT is a fictional or placeholder library. Substitute with actual library as needed
using IronPPT;

// Create a new instance of the PresentationDocument class, assuming PresentationDocument 
// is a part of IronPPT that helps create or modify PowerPoint presentations
var document = new PresentationDocument();

// Add a new slide to the presentation, assuming the Add method adds a new slide to the collection
document.Slides.Add(new Slide());

// Check if there is at least one slide before attempting to remove
if (document.Slides.Count > 0)
{
    // Remove the first slide from the presentation's list of slides
    document.Slides.RemoveAt(0);
}

// Save the modified presentation to a file named "removeSlide.pptx"
// The Save method will write the current state of the presentation to the specified file
document.Save("removeSlide.pptx");
' Import the IronPPT namespace to handle PowerPoint presentations
' Assuming IronPPT is a fictional or placeholder library. Substitute with actual library as needed
Imports IronPPT

' Create a new instance of the PresentationDocument class, assuming PresentationDocument 
' is a part of IronPPT that helps create or modify PowerPoint presentations
Private document = New PresentationDocument()

' Add a new slide to the presentation, assuming the Add method adds a new slide to the collection
document.Slides.Add(New Slide())

' Check if there is at least one slide before attempting to remove
If document.Slides.Count > 0 Then
	' Remove the first slide from the presentation's list of slides
	document.Slides.RemoveAt(0)
End If

' Save the modified presentation to a file named "removeSlide.pptx"
' The Save method will write the current state of the presentation to the specified file
document.Save("removeSlide.pptx")
$vbLabelText   $csharpLabel

如何在 PowerPoint 中重新排序幻燈片?

重新排列幻燈片順序,以更符合簡報流程。 重新排列投影片既簡單又高效,可以輕鬆更新思路順序或適應新的要求。 在從範本產生簡報或最佳幻燈片順序取決於動態因素(如觀眾類型或簡報情境)時,此功能證明非常有價值。 檢查 changelog 以取得幻燈片重新排序功能的最新更新。

在不同位置之間移動幻燈片的最佳方式是什麼?

使用 Remove()Insert() 方法從目前位置移除幻燈片,並在所需的索引位置插入幻燈片。 這兩個步驟可確保在不重複幻燈片的情況下進行乾淨的重新定位。 當執行複雜的重新排序邏輯時,在套用變更之前先建立臨時集合來規劃新的順序。 此方法可將錯誤減至最低,並使重新排序邏輯更容易測試和除錯。

重新排序時如何驗證索引位置?

確保目標索引在有效範圍內(0Slides.Count),以防止在進行幻燈片重新排序作業時發生執行時異常。 實施全面的驗證,考慮到邊緣情況,例如將幻燈片移動到目前位置,或嘗試將上一張幻燈片移動到集合界限之外。 考慮建立擴充方法,提供安全的重新排序與內建的驗證,並提供有意義的錯誤訊息以利除錯。

:path=/static-assets/ppt/content-code-examples/how-to/manage-slide-reorder-slide.cs
using IronPPT;

var document = new PresentationDocument();

// Adding a new slide to the document.
document.AddSlide();

// To reorder slides, we must remove the slide from its current position 
// and then insert it back at the desired position. 

// Capture the slide to be moved. 
// Assuming we want to move the first slide in this case.
var slideToMove = document.Slides[0];

// Remove the slide from its current position.
document.Slides.Remove(slideToMove);

// Add the slide back at the desired index (for example, index 1).
// Ensure the desired index is valid and within the range of the current slides.
if (document.Slides.Count >= 1) // Check if there is at least one slide to insert into.
{
    document.Slides.Insert(1, slideToMove);
}

// Save the presentation with the reordered slide.
// Ensure a valid file path and name are provided.
document.Save("reorderSlide.pptx");
Imports IronPPT

Private document = New PresentationDocument()

' Adding a new slide to the document.
document.AddSlide()

' To reorder slides, we must remove the slide from its current position 
' and then insert it back at the desired position. 

' Capture the slide to be moved. 
' Assuming we want to move the first slide in this case.
Dim slideToMove = document.Slides(0)

' Remove the slide from its current position.
document.Slides.Remove(slideToMove)

' Add the slide back at the desired index (for example, index 1).
' Ensure the desired index is valid and within the range of the current slides.
If document.Slides.Count >= 1 Then ' Check if there is at least one slide to insert into.
	document.Slides.Insert(1, slideToMove)
End If

' Save the presentation with the reordered slide.
' Ensure a valid file path and name are provided.
document.Save("reorderSlide.pptx")
$vbLabelText   $csharpLabel

如何隱藏幻燈片而不刪除它們?

隱藏特定投影片,但保留它們在簡報中。 隱藏的幻燈片不會在幻燈片秀中顯示,但仍可存取以供編輯或日後使用。 此功能可根據簡報內容保留可能需要的備份內容、演講者註解或替代幻燈片版本。 隱藏式幻燈片消耗最少的資源,並提供動態簡報的彈性。 有關支援進階幻燈片管理功能的 授權選項,請參閱定價頁面。

為什麼我要隱藏幻燈片而不是刪除它們?

隱藏的幻燈片可保留備份內容、講者註解或替代版本,同時使其不在主要簡報流程中,以達到更乾淨的傳達效果。 在為不同受眾維護多個內容版本或保留歷史資訊時,這種方法非常有效。 隱藏的投影片可作為範本或參考資料,主講人可在問答環節中解除隱藏。 考慮實施幻燈片標籤系統,以有效分類和管理隱藏的幻燈片。

隱藏的幻燈片可以程式化存取嗎?

是的,隱藏的投影片仍可透過程式碼完全存取,讓您隨時取消隱藏、修改或引用其內容。這種程式化的存取方式可實現複雜的簡報工作流程,讓幻燈片根據執行時的條件動態顯示或隱藏。 實施幻燈片可視性管理系統,可根據使用者角色、簡報模式或外部資料來源切換可視性。 對於需要進階許可功能的企業應用程式,請探索許可延伸升級選項

:path=/static-assets/ppt/content-code-examples/how-to/manage-slide-hide-slide.cs
using IronPPT;

// Create a new presentation document
var document = new PresentationDocument();

// Add a new slide to the presentation
document.AddSlide();

// Hide the first slide by setting its visibility to false
document.Slides[0].Visible = false;

// Save the presentation to a file named 'hideSlide.pptx'
document.Save("hideSlide.pptx");
Imports IronPPT

' Create a new presentation document
Private document = New PresentationDocument()

' Add a new slide to the presentation
document.AddSlide()

' Hide the first slide by setting its visibility to false
document.Slides(0).Visible = False

' Save the presentation to a file named 'hideSlide.pptx'
document.Save("hideSlide.pptx")
$vbLabelText   $csharpLabel

常見問題解答

如何使用 C# 程式化地在 PowerPoint 簡報中加入幻燈片?

您可以使用 IronPPT 的 AddSlide() 方法新增幻燈片。新的幻燈片會自動追加到簡報的末尾。對於多張幻燈片,只要連續呼叫 AddSlide(),或使用循環進行有效率的批次操作即可。

我可以從 PowerPoint 簡報中移除特定的投影片嗎?

是的,IronPPT 允許您使用 Remove() 方法移除幻燈片。只需透過 Slides 集合使用其索引存取幻燈片 (例如,Slides[0].Remove() 移除第一張幻燈片)。

如何使用 C# 在 PowerPoint 簡報中重新排序幻燈片?

IronPPT 提供對幻燈片集合的存取,可讓您以程式化的方式重新排列幻燈片。您可以使用集合的索引系統來操作幻燈片的位置,其中幻燈片是以 0 為基礎的(第一張幻燈片是索引 0)。

是否可以隱藏幻燈片而不刪除它們?

是的,IronPPT 支援以程式方式隱藏幻燈片。當您想要暫時從簡報中排除幻燈片,但又不想將它們從檔案中永久移除時,這項功能非常有用。

程式化幻燈片管理的實際應用為何?

IronPPT 可實現報表生成的自動化、從資料來源建立動態簡報,以及消除重複的手動 PowerPoint 編輯工作。這對於需要自動化簡報產生的商業應用程式來說特別有價值。

使用幻燈片管理功能需要授權嗎?

雖然 IronPPT 的幻燈片管理功能完備,但您需要將授權金鑰設定為生產使用,以避免在生成的簡報上出現水印。

Curtis Chau
技術撰稿人

Curtis Chau 擁有電腦科學學士學位(卡爾頓大學),專長於前端開發,精通 Node.js、TypeScript、JavaScript 和 React。Curtis 對製作直覺且美觀的使用者介面充滿熱情,他喜歡使用現代化的架構,並製作結構良好且視覺上吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 也有濃厚的興趣,他喜歡探索整合硬體與軟體的創新方式。在空閒時間,他喜歡玩遊戲和建立 Discord bots,將他對技術的熱愛與創意結合。

準備好開始了嗎?
Nuget 下載 4,038 | 版本: 2026.2 剛剛發布