Đầu trang và chân trang trong bản trình bày PowerPoint được sử dụng để hiển thị thông tin bổ sung như số trang chiếu, tác giả, ngày tháng, v.v. Trong bài viết này, bạn sẽ học cách thêm và quản lý đầu trang và chân trang trong bản trình bày PowerPoint PPTX / PPT theo lập trình bằng Java.
- API Java để quản lý đầu trang và chân trang trong PowerPoint
- Thêm Đầu trang và Chân trang trong Bản trình bày PowerPoint
- Quản lý Header và Footer trong Handout và Notes Slide
API Java để quản lý đầu trang và chân trang trong PowerPoint
Để làm việc với đầu trang và chân trang trong bản trình bày PowerPoint, chúng tôi sẽ sử dụng Aspose.Slides for Java. Đây là một API giàu tính năng cho phép bạn tạo và thao tác với các tài liệu PowerPoint và OpenOffice. Bạn có thể tải xuống JAR của API hoặc cài đặt nó qua Maven.
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-slides</artifactId>
<version>21.9</version>
<classifier>jdk16</classifier>
</dependency>
Thêm Đầu trang và Chân trang trong PowerPoint bằng Java
Sau đây là các bước để thêm đầu trang và chân trang trong bản trình bày PowerPoint bằng Java.
- Đầu tiên, tạo bản trình bày mới hoặc tải bản trình bày hiện có bằng cách sử dụng lớp Trình bày.
- Sau đó, đặt chân trang bằng phương thức Presentation.getHeaderFooterManager(). SetAllFootersText (String).
- Truy cập trang chiếu ghi chú chính trong đối tượng IMasterNotesSlide bằng phương thức Presentation.getMasterNotesSlideManager(). GetMasterNotesSlide().
- Lặp qua từng hình dạng trong bộ sưu tập IMasterNotesSlide.getShapes().
- Nếu IShape.getPlaceholder(). GetType() là PlaceholderType.Header thì hãy đặt văn bản cho tiêu đề bằng phương thức ((IAutoShape) shape) .getTextFrame(). SetText() .
- Cuối cùng, lưu bản trình bày bằng phương thức Presentation.save (string, SaveFormat).
Mẫu mã sau đây cho biết cách thêm đầu trang và chân trang trong bản trình bày PowerPoint.
// Tải bản trình bày
Presentation pres = new Presentation("headerTest.pptx");
try {
// Đặt chân trang
pres.getHeaderFooterManager().setAllFootersText("My Footer text");
pres.getHeaderFooterManager().setAllFootersVisibility(true);
// Truy cập và cập nhật tiêu đề
IMasterNotesSlide masterNotesSlide = pres.getMasterNotesSlideManager().getMasterNotesSlide();
if (null != masterNotesSlide)
{
for(IShape shape : masterNotesSlide.getShapes())
{
if (shape.getPlaceholder() != null)
{
if (shape.getPlaceholder().getType() == PlaceholderType.Header)
{
((IAutoShape)shape).getTextFrame().setText("HI there new header");
}
}
}
}
// Lưu bản trình bày
pres.save("HeaderFooterJava.pptx", SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Quản lý Header và Footer trong Handout và Notes Slide bằng Java
Aspose.Slides for Java cũng cho phép bạn đặt đầu trang và chân trang trong các trang trình bày tài liệu phát tay và ghi chú. Đối với điều này, bạn có thể áp dụng các thay đổi trong trang chiếu ghi chú chính hoặc một trang chiếu riêng lẻ. Các phần sau bao gồm cả hai tình huống.
Thay đổi cài đặt đầu trang và chân trang cho Notes Master
- Trước tiên, hãy tạo một bản trình bày mới hoặc tải một bản trình bày hiện có bằng cách sử dụng lớp Trình bày.
- Sau đó, truy cập trang chiếu ghi chú chính trong đối tượng IMasterNotesSlide bằng phương thức Presentation.getMasterNotesSlideManager(). GetMasterNotesSlide().
- Tham khảo IMasterNotesSlideHeaderFooterManager từ phương thức IMasterNotesSlide.getHeaderFooterManager().
- Cập nhật chân trang đầu trang bằng đối tượng IMasterNotesSlideHeaderFooterManager.
- Cuối cùng, lưu bản trình bày bằng phương thức Presentation.save (string, SaveFormat).
Mẫu mã sau đây cho thấy cách thay đổi đầu trang và chân trang trong phần mềm ghi chú bằng cách sử dụng Java.
// Tải bản trình bày
Presentation pres = new Presentation("presentation.pptx");
try {
// Thay đổi cài đặt Đầu trang và Chân trang cho chính ghi chú và tất cả các trang trình bày ghi chú
IMasterNotesSlide masterNotesSlide = pres.getMasterNotesSlideManager().getMasterNotesSlide();
if (masterNotesSlide != null)
{
IMasterNotesSlideHeaderFooterManager headerFooterManager = masterNotesSlide.getHeaderFooterManager();
headerFooterManager.setHeaderAndChildHeadersVisibility(true); // make the master notes slide and all child Footer placeholders visible
headerFooterManager.setFooterAndChildFootersVisibility(true); // make the master notes slide and all child Header placeholders visible
headerFooterManager.setSlideNumberAndChildSlideNumbersVisibility(true); // make the master notes slide and all child SlideNumber placeholders visible
headerFooterManager.setDateTimeAndChildDateTimesVisibility(true); // make the master notes slide and all child Date and time placeholders visible
headerFooterManager.setHeaderAndChildHeadersText("Header text"); // set text to master notes slide and all child Header placeholders
headerFooterManager.setFooterAndChildFootersText("Footer text"); // set text to master notes slide and all child Footer placeholders
headerFooterManager.setDateTimeAndChildDateTimesText("Date and time text"); // set text to master notes slide and all child Date and time placeholders
}
// Lưu bản trình bày
pres.save("testresult.pptx",SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Thay đổi Cài đặt Đầu trang và Chân trang cho Trang trình bày Ghi chú
- Đầu tiên, hãy tạo một bản trình bày mới hoặc tải một bản trình bày hiện có bằng cách sử dụng lớp Trình bày.
- Sau đó, truy cập đối tượng INotesSlide của trang trình bày mong muốn bằng phương thức Presentation.getSlides(). GetItem (int index) .getNotesSlideManager(). GetNotesSlide().
- Tham khảo INotesSlideHeaderFooterManager từ phương thức INotesSlide.getHeaderFooterManager().
- Cập nhật chân trang đầu trang bằng đối tượng INotesSlideHeaderFooterManager.
- Cuối cùng, lưu bản trình bày bằng phương thức Presentation.save (string, SaveFormat).
Mẫu mã sau đây cho thấy cách thay đổi đầu trang và chân trang trong các trang trình bày ghi chú bằng Java.
// Tải bản trình bày
Presentation pres = new Presentation("presentation.pptx");
try {
// Thay đổi cài đặt Đầu trang và Chân trang chỉ cho trang chiếu ghi chú đầu tiên
INotesSlide notesSlide = pres.getSlides().get_Item(0).getNotesSlideManager().getNotesSlide();
if (notesSlide != null)
{
INotesSlideHeaderFooterManager headerFooterManager = notesSlide.getHeaderFooterManager();
if (!headerFooterManager.isHeaderVisible())
headerFooterManager.setHeaderVisibility(true); // make this notes slide Header placeholder visible
if (!headerFooterManager.isFooterVisible())
headerFooterManager.setFooterVisibility(true); // make this notes slide Footer placeholder visible
if (!headerFooterManager.isSlideNumberVisible())
headerFooterManager.setSlideNumberVisibility(true); // make this notes slide SlideNumber placeholder visible
if (!headerFooterManager.isDateTimeVisible())
headerFooterManager.setDateTimeVisibility(true); // make this notes slide Date-time placeholder visible
headerFooterManager.setHeaderText("New header text"); // set text to notes slide Header placeholder
headerFooterManager.setFooterText("New footer text"); // set text to notes slide Footer placeholder
headerFooterManager.setDateTimeText("New date and time text"); // set text to notes slide Date-time placeholder
}
// Lưu bản trình bày
pres.save("testresult.pptx",SaveFormat.Pptx);
} finally {
if (pres != null) pres.dispose();
}
Nhận giấy phép API miễn phí
Nhận giấy phép tạm thời miễn phí để sử dụng Aspose.Slides for Java mà không có giới hạn đánh giá.
Sự kết luận
Trong bài này, bạn đã học cách thêm đầu trang và chân trang trong bản trình bày PowerPoint bằng Java. Hơn nữa, bạn đã thấy cách thay đổi đầu trang và chân trang trong các trang trình bày ghi chú theo lập trình. Bên cạnh đó, bạn có thể khám phá tài liệu để tìm hiểu thêm về Aspose.Slides for Java. Ngoài ra, bạn có thể đặt câu hỏi của mình qua diễn đàn của chúng tôi.