在 Android 上管理演示文稿中的幻灯片节

简介

节将连续的幻灯片组织成具有名称的组,而不会更改幻灯片内容。使用 Aspose.Slides for Android via Java,您可以通过Presentation.getSections方法创建、重新排序、重命名、检查和删除节。

在以下情况下,节特别有用:

  • 需要将大型演示文稿划分为逻辑主题或章节;
  • 不同的幻灯片组分配给不同的协作者;
  • 需要将幻灯片作为一组进行处理、移动或合并。

请选择能够描述分组幻灯片目的的简洁节名称。由于节是演示文稿结构的一部分,请使用节 API 来确定成员关系,而不是从幻灯片位置推断。

创建和管理节

使用ISectionCollection.addSection通过指定名称和起始幻灯片来创建节。Aspose.Slides 根据演示文稿当前的节结构确定哪些幻灯片属于该节。

相同的ISectionCollection还可以让您:

下面的示例创建了两个节,移动其中一个,连同其幻灯片一起删除它,并追加一个空节:

import com.aspose.slides.ILayoutSlide;
import com.aspose.slides.ISection;
import com.aspose.slides.ISlide;
import com.aspose.slides.Presentation;

Presentation presentation = new Presentation();
try {
    ISlide titleSlide = presentation.getSlides().get_Item(0);
    ILayoutSlide layoutSlide = presentation.getLayoutSlides().get_Item(0);
    presentation.getSlides().addEmptySlide(layoutSlide);
    ISlide resultsSlide = presentation.getSlides().addEmptySlide(layoutSlide);
    presentation.getSlides().addEmptySlide(layoutSlide);

    presentation.getSections().addSection("Introduction", titleSlide);
    ISection resultsSection = presentation.getSections().addSection("Results", resultsSlide);

    presentation.getSections().reorderSectionWithSlides(resultsSection, 0);
    presentation.getSections().removeSectionWithSlides(resultsSection);
    presentation.getSections().appendEmptySection("Appendix");
} finally {
    presentation.dispose();
}

这些操作完成后,演示文稿包含带有幻灯片的 Introduction 节和一个空的 Appendix 节。Results 节及其幻灯片已被删除。

重命名节

要重命名节,调用其ISection.setName方法。节的幻灯片和位置保持不变。

下面的示例创建了一个节并更改其名称:

import com.aspose.slides.ISection;
import com.aspose.slides.ISlide;
import com.aspose.slides.Presentation;

Presentation presentation = new Presentation();
try {
    ISlide slide = presentation.getSlides().get_Item(0);
    ISection section = presentation.getSections().addSection("Overview", slide);
    section.setName("Introduction");
} finally {
    presentation.dispose();
}

从节检索幻灯片

Presentation.getSections方法返回一个可以遍历的ISectionCollection。对于每个ISection,调用ISection.getSlidesListOfSection可获取当前属于该节的幻灯片。该方法返回一个ISectionSlideCollection,提供计数、索引访问和迭代功能。

下面的示例创建了两个已填充的节和一个空节,然后打印每个节的nameidentifierstarting slide、幻灯片计数和幻灯片编号。它使用ISectionSlideCollection.get_Item读取第一张幻灯片,并使用增强的 for 语句处理每张幻灯片。对于空节,返回的集合大小为零,不调用该方法,迭代不执行任何操作。

import com.aspose.slides.ILayoutSlide;
import com.aspose.slides.ISection;
import com.aspose.slides.ISectionSlideCollection;
import com.aspose.slides.ISlide;
import com.aspose.slides.Presentation;

Presentation presentation = new Presentation();
try {
    ISlide firstSlide = presentation.getSlides().get_Item(0);
    ILayoutSlide layoutSlide = presentation.getLayoutSlides().get_Item(0);
    presentation.getSlides().addEmptySlide(layoutSlide);
    ISlide thirdSlide = presentation.getSlides().addEmptySlide(layoutSlide);

    presentation.getSections().addSection("Introduction", firstSlide);
    presentation.getSections().addSection("Details", thirdSlide);
    presentation.getSections().appendEmptySection("Appendix");

    for (ISection section : presentation.getSections()) {
        ISectionSlideCollection sectionSlides = section.getSlidesListOfSection();
        String startingSlide = section.getStartedFromSlide() == null ? "none" : Integer.toString(section.getStartedFromSlide().getSlideNumber());

        System.out.println("Section: " + section.getName());
        System.out.println("ID: " + section.getSectionId());
        System.out.println("Starting slide: " + startingSlide);
        System.out.println("Slide count: " + sectionSlides.size());

        if (sectionSlides.size() > 0) {
            System.out.println("First slide via get_Item: " + sectionSlides.get_Item(0).getSlideNumber());
        }

        System.out.print("Slide numbers:");
        for (ISlide slide : sectionSlides) {
            System.out.print(" " + slide.getSlideNumber());
        }
        System.out.println();
    }
} finally {
    presentation.dispose();
}

节成员关系由演示文稿的节结构决定。不要从ISection.getStartedFromSlide、幻灯片索引以及下一个节的起始幻灯片手动计算节的范围。

结构编辑可能会更改为节返回的幻灯片以及它们的幻灯片编号。这包括重新排序幻灯片、将幻灯片克隆到节、移动节及其幻灯片、删除幻灯片以及删除节。下一个示例在每次此类更改后调用ISection.getSlidesListOfSection,而不是保留对节先前边界的假设。

import com.aspose.slides.ILayoutSlide;
import com.aspose.slides.ISection;
import com.aspose.slides.ISectionSlideCollection;
import com.aspose.slides.ISlide;
import com.aspose.slides.Presentation;

import java.util.function.BiConsumer;

Presentation presentation = new Presentation();
try {
    ISlide firstSlide = presentation.getSlides().get_Item(0);
    ILayoutSlide layoutSlide = presentation.getLayoutSlides().get_Item(0);
    presentation.getSlides().addEmptySlide(layoutSlide);
    ISlide thirdSlide = presentation.getSlides().addEmptySlide(layoutSlide);
    presentation.getSlides().addEmptySlide(layoutSlide);
    ISection firstSection = presentation.getSections().addSection("First", firstSlide);
    ISection secondSection = presentation.getSections().addSection("Second", thirdSlide);

    BiConsumer<String, ISection> printSectionSlides = (label, section) -> {
        ISectionSlideCollection sectionSlides = section.getSlidesListOfSection();
        System.out.printf("%s (%d slides):", label, sectionSlides.size());
        for (ISlide slide : sectionSlides) {
            System.out.print(" " + slide.getSlideNumber());
        }
        System.out.println();
    };

    printSectionSlides.accept("Initially", firstSection);

    ISectionSlideCollection slidesBeforeClone = firstSection.getSlidesListOfSection();
    presentation.getSlides().addClone(slidesBeforeClone.get_Item(0), firstSection);
    printSectionSlides.accept("After cloning into the section", firstSection);

    ISectionSlideCollection slidesBeforeReorder = firstSection.getSlidesListOfSection();
    int firstSectionPosition = slidesBeforeReorder.get_Item(0).getSlideNumber() - 1;
    presentation.getSlides().reorder(firstSectionPosition, slidesBeforeReorder.get_Item(slidesBeforeReorder.size() - 1));
    printSectionSlides.accept("After reordering slides", firstSection);

    presentation.getSections().reorderSectionWithSlides(firstSection, 1);
    printSectionSlides.accept("After moving the section", firstSection);

    ISectionSlideCollection slidesBeforeRemoval = firstSection.getSlidesListOfSection();
    presentation.getSlides().remove(slidesBeforeRemoval.get_Item(0));
    printSectionSlides.accept("After removing a slide", firstSection);

    presentation.getSections().removeSectionWithSlides(secondSection);
    for (ISection section : presentation.getSections()) {
        printSectionSlides.accept("Remaining section", section);
    }
} finally {
    presentation.dispose();
}

每当幻灯片或节被重新排序、克隆、移动或删除时,请再次调用ISection.getSlidesListOfSection。这可确保后续处理与当前演示文稿结构保持一致。

PPT(PowerPoint 97–2003)格式不保留节元数据。请在支持节的格式(如 PPTX)中使用此工作流;转换为 PPT 会删除后续迭代所需的节结构。

常见问题

将演示文稿保存为 PPT(PowerPoint 97–2003)格式时,节会被保留下来吗?

不会。PPT 格式不支持节元数据,因此在保存为 .ppt 时节分组会丢失。

可以将整个节“隐藏”吗?

不能。节没有可见性状态。若要隐藏其内容,需要对该节中的每张幻灯片调用ISlide.setHidden

如何找到包含特定幻灯片的节?

遍历Presentation.getSections返回的集合,对每个节调用ISection.getSlidesListOfSection并将返回的幻灯片与目标幻灯片进行比较。对于非空节,ISection.getStartedFromSlide 返回其第一张幻灯片;对于空节,则返回 null