Đầ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.

Để 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>

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.

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

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.

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

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.

Xem thêm