在 .NET 中套用或變更投影片版面配置

簡介

投影片版面配置定義了投影片上佔位方塊的排列方式以及內容的格式設定。它控制哪些佔位元可用及其顯示位置。投影片版面配置可協助您快速且一致地設計簡報,無論是簡單或較複雜的情況。PowerPoint 中最常見的投影片版面配置包括:

標題投影片版面配置 – 包含兩個文字佔位元:一個用於標題,另一個用於副標題。

標題與內容版面配置 – 於上方提供較小的標題佔位元,下方提供較大的主內容佔位元(例如文字、項目符號、圖表、圖片等)。

空白版面配置 – 不含任何佔位元,讓您能從頭自行設計投影片。

投影片版面配置是投影片母片的一部份,母片是定義簡報版面樣式的最高層投影片。您可以透過投影片母片存取與修改版面投影片——依類型、名稱或唯一 ID 皆可。或者,您也可以直接在簡報內編輯特定的版面投影片。

若要在 Aspose.Slides for .NET 中操作投影片版面配置,您可以使用:

將版面配置新增至簡報

若要自訂投影片的外觀與結構,您可能需要在簡報中加入新的版面投影片。Aspose.Slides for .NET 允許您檢查特定版面是否已存在,必要時新增,並以該版面插入投影片。

  1. 建立 Presentation 類別的實例。
  2. 取得 IMasterLayoutSlideCollection
  3. 檢查所需的版面投影片是否已存在於集合中。若不存在,則新增所需的版面投影片。
  4. 新增一張基於新版面投影片的空白投影片。
  5. 儲存簡報。

以下 C# 程式碼示範如何將版面配置加入 PowerPoint 簡報:

// 實例化代表 PowerPoint 檔案的 Presentation 類別。
using (Presentation presentation = new Presentation("Sample.pptx"))
{
    // 遍歷版面投影片類型以選擇版面投影片。
    IMasterLayoutSlideCollection layoutSlides = presentation.Masters[0].LayoutSlides;
    ILayoutSlide layoutSlide = layoutSlides.GetByType(SlideLayoutType.TitleAndObject) ?? layoutSlides.GetByType(SlideLayoutType.Title);

    if (layoutSlide == null)
    {
        // 簡報不包含所有版面類型的情況。
        // 簡報檔案僅包含空白和自訂版面類型。
        // 但是,自訂類型的版面投影片可能具有可辨識的名稱,
        // 例如 "Title"、"Title and Content" 等,可用於版面投影片的選取。
        // 您也可以依賴一組佔位元形狀類型。
        // 例如,標題投影片應僅有 Title 佔位元類型,依此類推。
        foreach (ILayoutSlide titleAndObjectLayoutSlide in layoutSlides)
        {
            if (titleAndObjectLayoutSlide.Name == "Title and Object")
            {
                layoutSlide = titleAndObjectLayoutSlide;
                break;
            }
        }

        if (layoutSlide == null)
        {
            foreach (ILayoutSlide titleLayoutSlide in layoutSlides)
            {
                if (titleLayoutSlide.Name == "Title")
                {
                    layoutSlide = titleLayoutSlide;
                    break;
                }
            }

            if (layoutSlide == null)
            {
                layoutSlide = layoutSlides.GetByType(SlideLayoutType.Blank);
                if (layoutSlide == null)
                {
                    layoutSlide = layoutSlides.Add(SlideLayoutType.TitleAndObject, "Title and Object");
                }
            }
        }
    }

    // 使用新增的版面投影片插入空白投影片。
    presentation.Slides.InsertEmptySlide(0, layoutSlide);

    // 將簡報儲存至磁碟。  
    presentation.Save("Output.pptx", SaveFormat.Pptx);
}

移除未使用的版面投影片

Aspose.Slides 透過 Compress 類別的 RemoveUnusedLayoutSlides 方法,讓您刪除不需要且未被使用的版面投影片。

以下 C# 程式碼示範如何從 PowerPoint 簡報中移除版面投影片:

using (Presentation presentation = new Presentation("Presentation.pptx"))
{
    Aspose.Slides.LowCode.Compress.RemoveUnusedLayoutSlides(presentation);
    
    presentation.Save("Output.pptx", SaveFormat.Pptx);
}

向版面投影片新增佔位元

Aspose.Slides 提供 ILayoutSlide.PlaceholderManager 屬性,讓您能在版面投影片上新增佔位元。

此管理器提供以下佔位元類型的方法:

PowerPoint 佔位元 ILayoutPlaceholderManager 方法
內容 AddContentPlaceholder(float x, float y, float width, float height)
內容(垂直) AddVerticalContentPlaceholder(float x, float y, float width, float height)
文字 AddTextPlaceholder(float x, float y, float width, float height)
文字(垂直) AddVerticalTextPlaceholder(float x, float y, float width, float height)
圖片 AddPicturePlaceholder(float x, float y, float width, float height)
圖表 AddChartPlaceholder(float x, float y, float width, float height)
表格 AddTablePlaceholder(float x, float y, float width, float height)
SmartArt AddSmartArtPlaceholder(float x, float y, float width, float height)
媒體 AddMediaPlaceholder(float x, float y, float width, float height)
線上圖片 AddOnlineImagePlaceholder(float x, float y, float width, float height)

以下 C# 程式碼示範如何在「空白」版面投影片上新增佔位元圖形:

using (var presentation = new Presentation())
{
    // 取得空白版面投影片。
    ILayoutSlide layout = presentation.LayoutSlides.GetByType(SlideLayoutType.Blank);

    // 取得版面投影片的佔位元管理器。
    ILayoutPlaceholderManager placeholderManager = layout.PlaceholderManager;

    // 為空白版面投影片新增不同的佔位元。
    placeholderManager.AddContentPlaceholder(20, 20, 310, 270);
    placeholderManager.AddVerticalTextPlaceholder(350, 20, 350, 270);
    placeholderManager.AddChartPlaceholder(20, 310, 310, 180);
    placeholderManager.AddTablePlaceholder(350, 310, 350, 180);

    // 使用空白版面新增一張投影片。
    ISlide newSlide = presentation.Slides.AddEmptySlide(layout);

    presentation.Save("Placeholders.pptx", SaveFormat.Pptx);
}

結果:

版面配置投影片上的佔位元

設定版面投影片的頁腳可見性

在 PowerPoint 簡報中,日期、投影片編號與自訂文字等頁腳元素可依版面配置顯示或隱藏。Aspose.Slides for .NET 讓您控制這些頁腳佔位元的可見性,適用於需要在特定版面顯示頁腳資訊,而其他版面保持簡潔的情況。

  1. 建立 Presentation 類別的實例。
  2. 依索引取得版面投影片的參考。
  3. 設定投影片頁腳佔位元為可見。
  4. 設定投影片編號佔位元為可見。
  5. 設定日期時間佔位元為可見。
  6. 儲存簡報。

以下 C# 程式碼示範如何設定投影片頁腳的可見性及相關操作:

using (Presentation presentation = new Presentation("Presentation.ppt"))
{
    ILayoutSlideHeaderFooterManager headerFooterManager = presentation.LayoutSlides[0].HeaderFooterManager;

    if (!headerFooterManager.IsFooterVisible)
    {
        headerFooterManager.SetFooterVisibility(true);
    }

    if (!headerFooterManager.IsSlideNumberVisible)
    {
        headerFooterManager.SetSlideNumberVisibility(true);
    }

    if (!headerFooterManager.IsDateTimeVisible)
    {
        headerFooterManager.SetDateTimeVisibility(true);
    }

    headerFooterManager.SetFooterText("Footer text");
    headerFooterManager.SetDateTimeText("Date and time text");

    presentation.Save("Presentation.ppt", SaveFormat.Ppt);
}

設定子版面投影片的頁腳可見性

在 PowerPoint 簡報中,日期、投影片編號與自訂文字等頁腳元素可在母片層級設定,以確保所有版面投影片的一致性。Aspose.Slides for .NET 允許您在母片上設定這些頁腳佔位元的可見性與內容,並將設定傳播至所有子版面投影片,從而在整個簡報中維持統一的頁腳資訊。

  1. 建立 Presentation 類別的實例。
  2. 依索引取得母片投影片的參考。
  3. 將母片及所有子版面的頁腳佔位元設為可見。
  4. 將母片及所有子版面的投影片編號佔位元設為可見。
  5. 將母片及所有子版面的日期時間佔位元設為可見。
  6. 儲存簡報。

以下 C# 程式碼示範此操作:

using (Presentation presentation = new Presentation("Presentation.ppt"))
{
    IMasterSlideHeaderFooterManager headerFooterManager = presentation.Masters[0].HeaderFooterManager;

    headerFooterManager.SetFooterAndChildFootersVisibility(true);
    headerFooterManager.SetSlideNumberAndChildSlideNumbersVisibility(true);
    headerFooterManager.SetDateTimeAndChildDateTimesVisibility(true);

    headerFooterManager.SetFooterAndChildFootersText("Footer text");
    headerFooterManager.SetDateTimeAndChildDateTimesText("Date and time text");

    presentation.Save("Output.pptx", SaveFormat.Pptx);
}

常見問題

主片與版面投影片有何不同?

主片定義整體主題與預設格式,版面投影片則為不同類型內容定義特定的佔位元排列。

我可以將版面投影片從一個簡報複製到另一個嗎?

可以,您可以從一個簡報的 LayoutSlides 集合中克隆版面投影片,然後使用 AddClone 方法將其插入另一個簡報。

如果刪除仍被投影片使用的版面投影片會發生什麼事?

若嘗試刪除仍被至少一張投影片參照的版面投影片,Aspose.Slides 會拋出 PptxEditException。為避免此問題,請使用 RemoveUnusedLayoutSlides,它只會安全地移除未被使用的版面投影片。