在 Android 上应用或更改幻灯片布局

概述

幻灯片布局定义了标题、文本、图片、图表和表格等占位符的位置和格式。应用布局可以为幻灯片提供一致的结构,同时允许每张幻灯片包含其自己的内容。

最常用的布局包括:

  • 标题幻灯片:包含标题和副标题占位符。
  • 标题和内容:包含标题占位符和通用内容占位符。
  • 空白:不包含任何内容占位符,适用于需要手动定位每个形状的情况。

了解布局继承

演示文稿有三个相关层级:

  1. 一个母版幻灯片定义主题、共享格式、背景和公共对象。
  2. 一个布局幻灯片属于母版,定义特定的占位符排列。
  3. 一个普通幻灯片使用一个布局并存储该幻灯片的内容。

普通幻灯片从其布局继承主题和格式,布局则从其母版继承。直接在普通幻灯片上设置的值会覆盖该层级的继承值。创建普通幻灯片时,其占位符形状是根据所选布局生成的,而填入这些占位符的内容属于普通幻灯片。

在从布局创建幻灯片之前,请先向布局添加所需的占位符。随后再向布局添加占位符不会自动为已有的普通幻灯片添加相应的占位符形状。

此关系有两个重要后果:

  • 更改布局上继承的格式或现有占位符的几何形状会更新所有依赖该布局的幻灯片。在编辑已在使用的布局之前,请检查其依赖的幻灯片并预览生成的演示文稿。
  • 仍被幻灯片使用的布局无法删除。请先将其依赖的幻灯片重新分配到其他布局,或仅删除未使用的布局。

有关此层级顶部的更多信息,请参阅幻灯片母版

选择并应用幻灯片布局

当演示文稿遵循标准 PowerPoint 布局定义时,请使用布局类型。布局名称可由用户编辑并本地化,因此除非您控制源模板,否则基于名称的选择可靠性较低。

下面的示例在第一个母版上查找标题和内容布局。如果该布局不可用,则有意回退到空白。第二次空检查是必要的,因为演示文稿可能只包含自定义布局。随后通过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,随后基于该布局添加普通幻灯片。布局名称在集合内必须唯一。

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 方法
内容 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 能在该幻灯片上生成相应的占位符形状。

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

要删除特定布局,首先使用其hasDependingSlidesgetDependingSlides方法。在调用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只删除未被引用的布局。