在 PHP 中套用或變更投影片版面

概覽

投影片版面定義了標題、文字、圖片、圖表和表格等佔位符的位置與格式。套用版面可使投影片具備一致的結構,同時允許每張投影片保有各自的內容。

最常見的版面包括:

  • 標題投影片:包含標題與副標題佔位符。
  • 標題與內容:包含標題佔位符與通用內容佔位符。
  • 空白:不包含任何內容佔位符,適用於需要手動定位所有形狀的情況。

瞭解版面繼承

簡報具有三個相關層級:

  1. 一個母片投影片 定義主題、共用格式、背景及共通物件。
  2. 一個版面投影片 屬於母片,定義特定的佔位符配置。
  3. 一個普通投影片 使用一個版面,並儲存該投影片的內容。

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

在建立投影片之前,請先在版面中加入所需的佔位符。之後再為版面新增佔位符不會自動為現有的普通投影片添加對應的佔位符形狀。

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

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

欲瞭解此層級階層的更多資訊,請參閱投影片母片。

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

選取與套用投影片版面

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

以下範例在第一個母片上尋找標題與內容版面。如果該版面不存在,則會明確回退到空白。第二個 null 檢查是必要的,因為簡報可能只包含自訂版面。選取的版面随后透過Slide.setLayoutSlide 方法套用到第一張普通投影片。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\SlideLayoutType;

$presentation = new Presentation("input.pptx");
try {
    $layoutSlides = $presentation->getMasters()->get_Item(0)->getLayoutSlides();
    $targetLayout = $layoutSlides->getByType(SlideLayoutType::TitleAndObject);

    if (java_is_null($targetLayout)) {
        $targetLayout = $layoutSlides->getByType(SlideLayoutType::Blank);
    }

    if (java_is_null($targetLayout)) {
        throw new \RuntimeException("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();
}

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

新增版面投影片

選取與建立是分開的操作。前面的範例只選取了既有版面,並未建立。若要建立版面,請於目標母片的版面集合呼叫MasterLayoutSlideCollection.add 方法。

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

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\SlideLayoutType;

$presentation = new Presentation("input.pptx");
try {
    $masterSlide = $presentation->getMasters()->get_Item(0);
    $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();
}

僅在範本真正需要另一個可重複使用的結構時才新增版面。若已有合適的版面,請選取並重複使用,而非建立重複的版面。

在版面投影片中新增佔位符

LayoutSlide.getPlaceholderManager 方法會回傳一個LayoutPlaceholderManager 物件,用於向版面新增佔位符形狀。

PowerPoint 佔位符 LayoutPlaceholderManager 方法
內容 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)

以下範例驗證空白版面是否存在,向其新增四個佔位符,然後建立使用修改後版面的普通投影片。順序故意安排在先新增佔位符,再建立普通投影片,讓 Aspose.Slides 能在該投影片上產生對應的佔位符形狀。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\SlideLayoutType;

$presentation = new Presentation();
try {
    $blankLayout = $presentation->getLayoutSlides()->getByType(SlideLayoutType::Blank);

    if (java_is_null($blankLayout)) {
        throw new \RuntimeException("The presentation does not contain a Blank layout slide.");
    }

    $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 方法移除沒有任何普通投影片參照的版面。該方法會保留仍在使用中的版面。

use aspose\slides\Compress;
use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

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

若要移除特定版面,首先使用其hasDependingSlides 或getDependingSlides 方法。重新指派所有依賴的投影片後,再呼叫LayoutSlide.remove。嘗試移除仍在使用的版面會拋出 PptxEditException。

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

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

以下範例安全地選取版面,並將其頁腳元素設為可見:

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;
use aspose\slides\SlideLayoutType;

$presentation = new Presentation("input.pptx");
try {
    $layoutSlide = $presentation->getLayoutSlides()->getByType(SlideLayoutType::TitleAndObject);

    if (java_is_null($layoutSlide)) {
        $layoutSlide = $presentation->getLayoutSlides()->getByType(SlideLayoutType::Blank);
    }

    if (java_is_null($layoutSlide)) {
        throw new \RuntimeException("The presentation does not contain a suitable layout slide.");
    }

    $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();
}

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

若要在母片層級上套用一致的頁腳設定,請使用MasterSlide.getHeaderFooterManager 方法。MasterSlideHeaderFooterManager 的傳播方法同時作用於母片、其依賴的版面投影片與普通投影片;不會只針對單一普通投影片。

use aspose\slides\Presentation;
use aspose\slides\SaveFormat;

$presentation = new Presentation("input.pptx");
try {
    $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 僅移除未被參照的版面。