Quản lý các phần slide trong bài thuyết trình với Java
Giới thiệu
Các phần tổ chức các slide liên tiếp thành các nhóm có tên mà không thay đổi nội dung slide. Với Aspose.Slides for Java, bạn có thể tạo, sắp xếp lại, đổi tên, kiểm tra và xóa các phần thông qua phương thức Presentation.getSections .
Các phần đặc biệt hữu ích khi:
- một bản trình bày lớn cần được chia thành các chủ đề hoặc chương logic;
- các nhóm slide khác nhau được giao cho các cộng tác viên khác nhau;
- các slide cần được xử lý, di chuyển hoặc hợp nhất dưới dạng nhóm.
Chọn các tên phần ngắn gọn mô tả mục đích của các slide được nhóm lại. Vì các phần là một phần của cấu trúc bản trình bày, hãy sử dụng các API phần để xác định thành viên thay vì suy ra từ vị trí slide.
Tạo và Quản lý Các Phần
Sử dụng ISectionCollection.addSection để tạo một phần bằng cách chỉ định tên và slide bắt đầu. Aspose.Slides xác định các slide thuộc phần dựa trên cấu trúc phần hiện tại của bản trình bày.
Cùng với ISectionCollection , bạn còn có thể:
- di chuyển một phần cùng với các slide của nó bằng cách sử dụng ISectionCollection.reorderSectionWithSlides;
- xóa chỉ định nghĩa phần bằng ISectionCollection.removeSection, giữ lại các slide;
- xóa một phần và các slide của nó bằng ISectionCollection.removeSectionWithSlides;
- thêm một phần trống ở cuối bằng ISectionCollection.appendEmptySection.
Ví dụ sau tạo hai phần, di chuyển một trong số chúng, xóa nó cùng với các slide, và thêm một phần trống:
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();
}
Sau các thao tác này, bản trình bày chứa phần Introduction với các slide của nó và một phần Appendix trống. Phần Results và các slide của nó đã bị xóa.
Đổi tên Các Phần
Để đổi tên một phần, gọi phương thức ISection.setName . Các slide và vị trí của phần không thay đổi.
Ví dụ sau tạo một phần và thay đổi tên của nó:
import com.aspose.slides.ISection;
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();
}
Lấy Các Slide từ Các Phần
Presentation.getSections trả về một ISectionCollection mà bạn có thể lặp qua. Đối với mỗi ISection, gọi ISection.getSlidesListOfSection để lấy các slide hiện đang thuộc về nó. Phương thức trả về một ISectionSlideCollection, cung cấp số lượng, truy cập theo chỉ mục và khả năng lặp.
Ví dụ sau tạo hai phần đã có nội dung và một phần trống, sau đó in ra name, identifier, starting slide, số slide và số thứ tự slide của mỗi phần. Nó sử dụng ISectionSlideCollection.get_Item để đọc slide đầu tiên và câu lệnh for mở rộng để xử lý mọi slide. Đối với phần trống, bộ sưu tập trả về có kích thước bằng không, phương thức không được gọi và việc lặp không thực hiện thao tác nào.
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();
}
Thành viên của phần được xác định bởi cấu trúc phần của bản trình bày. Không tự tính phạm vi của một phần bằng cách sử dụng ISection.getStartedFromSlide, chỉ mục slide và slide bắt đầu của phần tiếp theo.
Các chỉnh sửa cấu trúc có thể thay đổi cả các slide được trả về cho một phần và số thứ tự slide của chúng. Điều này bao gồm sắp xếp lại slide, sao chép một slide vào một phần, di chuyển một phần cùng với các slide của nó, xóa slide và xóa phần. Ví dụ tiếp theo gọi ISection.getSlidesListOfSection sau mỗi thay đổi như vậy thay vì giữ giả định về ranh giới trước đây của phần.
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();
}
Gọi lại ISection.getSlidesListOfSection bất cứ khi nào slide hoặc phần được sắp xếp lại, sao chép, di chuyển hoặc xóa. Điều này giúp việc xử lý tiếp theo đồng bộ với cấu trúc bản trình bày hiện tại.
Định dạng PPT (PowerPoint 97–2003) không lưu giữ siêu dữ liệu phần. Hãy sử dụng quy trình này với định dạng hỗ trợ phần, như PPTX; chuyển sang PPT sẽ loại bỏ cấu trúc phần cần thiết cho việc lặp sau này.
Câu hỏi thường gặp
Các phần có được giữ lại khi lưu dưới định dạng PPT (PowerPoint 97–2003) không?
Không. Định dạng PPT không hỗ trợ siêu dữ liệu phần, vì vậy việc nhóm phần sẽ bị mất khi lưu dưới dạng .ppt.
Có thể ẩn toàn bộ một phần không?
Không. Một phần không có trạng thái hiển thị. Để ẩn nội dung của nó, gọi ISlide.setHidden cho mỗi slide trong phần đó.
Làm thế nào tôi có thể tìm phần chứa một slide?
Lặp qua bộ sưu tập trả về bởi Presentation.getSections , gọi ISection.getSlidesListOfSection cho mỗi phần và so sánh các slide trả về với slide mục tiêu. Đối với phần không rỗng, ISection.getStartedFromSlide trả về slide đầu tiên; đối với phần rỗng, nó trả về null.