套用或變更 Android 上的投影片版面配置

概觀

投影片版面配置定義了佔位符(例如標題、文字、圖片、圖表和表格)的定位與格式。套用版面配置可為投影片提供一致的結構,同時允許每張投影片包含自己的內容。

最常見的版面配置包括:

  • Title Slide:包含標題與副標題佔位符。
  • Title and Content:包含一個標題佔位符和一個通用內容佔位符。
  • Blank:不包含任何內容佔位符,適用於需要手動定位每個圖形的情況。

了解版面繼承

簡報具有三個相關層級:

  1. 一個母片 定義主題、共用格式、背景與共用物件。
  2. 一個版面投影片 隸屬於母片,並定義特定的佔位符排列。
  3. 一個一般投影片 使用一個版面,並儲存該投影片輸入的內容。

一般投影片從其版面繼承主題與格式,版面則繼承自其母片。直接設定在一般投影片上的值會覆寫該層級繼承的值。建立一般投影片時,會根據所選版面產生佔位符圖形,而輸入於這些佔位符的內容屬於一般投影片。

在從版面建立投影片之前,請先在版面中加入必要的佔位符。之後再向版面加入佔位符不會自動為已存在的一般投影片新增對應的佔位符圖形。

此關係有兩個重要的結果:

  • 變更版面繼承的格式或現有佔位符的幾何形狀可能會更新所有依賴該版面的投影片。編輯已在使用的版面前,請先檢查其依賴的投影片並審視最終的簡報。
  • 仍被投影片使用的版面無法被移除。必須先將其依賴的投影片重新指派至其他版面,或僅移除未使用的版面。

如需了解此階層最高層的更多資訊,請參閱投影片母片。

若要隱藏單一投影片或透過共享版面中的繼承標誌或裝飾性母片圖形,請參閱控制母片圖形的可見性。此範例比較了使用相同母片的兩張投影片。

選取與套用投影片版面

當簡報遵循標準 PowerPoint 版面定義時,請使用版面類型。版面名稱可由使用者編輯且可本地化,除非您控制來源範本,否則僅依名稱選取的可靠性較低。

以下範例在第一個母片上搜尋 Title and Content。若找不到該版面,則主動回退至 Blank。第二個 null 檢查是必要的,因為簡報可能僅包含自訂版面。選取的版面再透過ISlide.setLayoutSlide 方法套用至第一張一般投影片。

import com.aspose.slides.*;

Presentation presentation = new Presentation("input.pptx");
try {
    IMasterLayoutSlideCollection layoutSlides = presentation.getMasters().get_Item(0).getLayoutSlides();
    ILayoutSlide targetLayout = layoutSlides.getByType(SlideLayoutType.TitleAndObject);

    if (targetLayout == null) {
        targetLayout = layoutSlides.getByType(SlideLayoutType.Blank);
    }

    if (targetLayout == null) {
        throw new IllegalStateException("The first master does not contain a suitable layout slide.");
    }

    presentation.getSlides().get_Item(0).setLayoutSlide(targetLayout);
    presentation.save("output-with-new-layout.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

變更投影片的版面不會移除直接加入投影片的普通圖形。然而,佔位符位置、繼承的格式以及現有佔位符與新版面之間的對應可能會改變,因此在切換差異較大的版面時請檢查輸出結果。

新增版面投影片

選取與建立是分開的操作。前一個範例僅選取現有版面,並未建立新版面。若要建立版面,請對目標母片的版面集合呼叫IMasterLayoutSlideCollection.add 方法。

以下範例始終新增一個名為 Report Title and Content 的 Title and Content 版面,然後基於它新增一般投影片。版面名稱在集合中必須唯一。

import com.aspose.slides.*;

Presentation presentation = new Presentation("input.pptx");
try {
    IMasterSlide masterSlide = presentation.getMasters().get_Item(0);
    ILayoutSlide reportLayout = masterSlide.getLayoutSlides().add(SlideLayoutType.TitleAndObject, "Report Title and Content");
    presentation.getSlides().addEmptySlide(reportLayout);

    presentation.save("output-with-report-layout.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

只有在範本真正需要另一個可重複使用的結構時才新增版面。如果已存在合適的版面,請直接選取並重複使用,而不是建立重複的版面。

向版面投影片新增佔位符

ILayoutSlide.getPlaceholderManager 方法提供一個ILayoutPlaceholderManager 用於向版面新增佔位符圖形。

PowerPoint 佔位符 ILayoutPlaceholderManager Method
內容 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)

以下範例驗證 Blank 版面是否存在,向其新增四個佔位符,然後建立使用修改後版面的一般投影片。順序刻意設計:先新增佔位符,再建立一般投影片,以便 Aspose.Slides 能在該投影片上產生對應的佔位符圖形。

import com.aspose.slides.*;

Presentation presentation = new Presentation();
try {
    ILayoutSlide blankLayout = presentation.getLayoutSlides().getByType(SlideLayoutType.Blank);

    if (blankLayout == null) {
        throw new IllegalStateException("The presentation does not contain a Blank layout slide.");
    }

    ILayoutPlaceholderManager placeholderManager = blankLayout.getPlaceholderManager();
    placeholderManager.addContentPlaceholder(20, 20, 310, 270);
    placeholderManager.addVerticalTextPlaceholder(350, 20, 350, 270);
    placeholderManager.addChartPlaceholder(20, 310, 310, 180);
    placeholderManager.addTablePlaceholder(350, 310, 350, 180);

    presentation.getSlides().addEmptySlide(blankLayout);
    presentation.save("output-with-placeholders.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

結果:

版面投影片上的佔位符

移除未使用的版面投影片

使用Compress.removeUnusedLayoutSlides 方法移除沒有任何一般投影片參照的版面。此方法會保留仍在使用中的版面。

import com.aspose.slides.*;

Presentation presentation = new Presentation("input.pptx");
try {
    Compress.removeUnusedLayoutSlides(presentation);
    presentation.save("output-without-unused-layouts.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

若要移除特定版面,首先使用其hasDependingSlides 或 getDependingSlides 方法。於呼叫ILayoutSlide.remove 前,先重新指派任何依賴的投影片。嘗試移除仍被使用的版面會拋出 PptxEditException。

控制版面投影片的頁腳可見性

版面擁有自己的頁腳、投影片編號與日期時間佔位符。使用ILayoutSlide.getHeaderFooterManager 方法可對單一版面控制這些佔位符。這在例如內容版面需要顯示頁腳,而標題版面則不需要時非常有用。

以下範例安全地選取一個版面,並使其頁腳元素可見:

import com.aspose.slides.*;

Presentation presentation = new Presentation("input.pptx");
try {
    ILayoutSlide layoutSlide = presentation.getLayoutSlides().getByType(SlideLayoutType.TitleAndObject);

    if (layoutSlide == null) {
        layoutSlide = presentation.getLayoutSlides().getByType(SlideLayoutType.Blank);
    }

    if (layoutSlide == null) {
        throw new IllegalStateException("The presentation does not contain a suitable layout slide.");
    }

    ILayoutSlideHeaderFooterManager headerFooterManager = layoutSlide.getHeaderFooterManager();
    headerFooterManager.setFooterVisibility(true);
    headerFooterManager.setSlideNumberVisibility(true);
    headerFooterManager.setDateTimeVisibility(true);
    headerFooterManager.setFooterText("Footer text");
    headerFooterManager.setDateTimeText("Date and time text");

    presentation.save("output-with-layout-footers.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

控制母片及其子版面的頁腳可見性

要在母片層級中套用一致的頁腳設定,請使用IMasterSlide.getHeaderFooterManager 方法。IMasterSlideHeaderFooterManager 的傳播方法會作用於母片以及其依賴的版面投影片與一般投影片;它們不會僅針對單一一般投影片。

import com.aspose.slides.*;

Presentation presentation = new Presentation("input.pptx");
try {
    IMasterSlideHeaderFooterManager headerFooterManager = presentation.getMasters().get_Item(0).getHeaderFooterManager();
    headerFooterManager.setFooterAndChildFootersVisibility(true);
    headerFooterManager.setSlideNumberAndChildSlideNumbersVisibility(true);
    headerFooterManager.setDateTimeAndChildDateTimesVisibility(true);
    headerFooterManager.setFooterAndChildFootersText("Footer text");
    headerFooterManager.setDateTimeAndChildDateTimesText("Date and time text");

    presentation.save("output-with-master-footers.pptx", SaveFormat.Pptx);
} finally {
    presentation.dispose();
}

常見問題

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

母片定義簡報的主題與共用格式。版面投影片屬於母片,定義一套可重複使用的佔位符排列。一般投影片使用這些版面,並儲存投影片特有的內容。

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

可以。使用addClone 方法將版面加入目標集合。跨簡報複製時,亦需確認來源版面使用的字型、主題、圖片與其他資源是否在目標簡報中可用。

當我修改已在使用的版面時會發生什麼?

依賴的投影片會繼承版面變更,除非它們在本地覆寫了受影響的格式或物件。佔位符的幾何形狀與繼承的樣式可能會同時在多張投影片上改變。編輯版面前,請使用getDependingSlides 識別受影響的投影片。

若移除仍在使用的版面會發生什麼?

Aspose.Slides 會拋出 PptxEditException。請先重新指派依賴的投影片,或使用removeUnusedLayoutSlides 只移除未被參照的版面。