Làm việc với Bình luận và Ghi chú Diễn giả — Aspose.Slides FOSS cho Java

Aspose.Slides FOSS for Java hỗ trợ hai loại chú thích: threaded slide comments (hiển thị trong chế độ xem xét) và speaker notes (hiển thị trong Presenter View và Notes pane).


Bình luận dạng chuỗi

Bình luận được gắn vào một slide và liên kết với một tác giả. Bộ sưu tập prs.getCommentAuthors() quản lý tất cả các tác giả; mỗi tác giả có một bộ sưu tập getComments() để thêm và đọc bình luận.

Thêm bình luận

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.drawing.PointF;
import org.aspose.slides.foss.export.SaveFormat;
import java.time.LocalDateTime;

try (Presentation prs = new Presentation()) {
    // Create a comment author with initials
    var author = prs.getCommentAuthors().addAuthor("Jane Smith", "JS");

    var slide = prs.getSlides().get(0);

    // Add a comment at position (2.0, 2.0) centimeters from the slide top-left
    author.getComments().addComment(
        "Please review the figures on this slide",
        slide,
        new PointF(2.0f, 2.0f),
        LocalDateTime.now()
    );

    prs.save("commented.pptx", SaveFormat.PPTX);
}

Vị trí PointF được tính bằng centimeters từ góc trên‑trái của slide (được lưu trữ nội bộ dưới dạng EMU; 1 cm = 360,000 EMU). Nhiều bình luận có thể được thêm vào cùng một slide bằng cách gọi lại addComment().

Nhiều tác giả và bình luận

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.drawing.PointF;
import org.aspose.slides.foss.export.SaveFormat;
import java.time.LocalDateTime;

try (Presentation prs = new Presentation()) {
    var author1 = prs.getCommentAuthors().addAuthor("Alice Brown", "AB");
    var author2 = prs.getCommentAuthors().addAuthor("Bob Davis", "BD");

    var slide = prs.getSlides().get(0);

    author1.getComments().addComment("Initial draft", slide, new PointF(1.0f, 1.0f), LocalDateTime.now());
    author2.getComments().addComment("Approved", slide, new PointF(3.0f, 1.0f), LocalDateTime.now());

    prs.save("multi-author.pptx", SaveFormat.PPTX);
}

Đọc bình luận từ tệp hiện có

import org.aspose.slides.foss.Presentation;

try (Presentation prs = new Presentation("commented.pptx")) {
    for (int a = 0; a < prs.getCommentAuthors().size(); a++) {
        var author = prs.getCommentAuthors().get(a);
        System.out.println("Author: " + author.getName() + " (" + author.getInitials() + ")");
        for (int c = 0; c < author.getComments().size(); c++) {
            var comment = author.getComments().get(c);
            System.out.println("  " + comment.getText());
        }
    }
}

Ghi chú người thuyết trình

Ghi chú người nói được lưu trữ trên cơ sở mỗi slide thông qua đối tượng NotesSlide. Truy cập nó qua slide.getNotesSlideManager().

Thêm Ghi chú Diễn giả vào Slide

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;

try (Presentation prs = new Presentation()) {
    var slide = prs.getSlides().get(0);
    slide.getShapes().addAutoShape(ShapeType.RECTANGLE, 50, 50, 400, 200);

    // Create the notes slide and set text
    var notes = slide.getNotesSlideManager().addNotesSlide();
    notes.getNotesTextFrame().setText(
        "Mention the Q3 revenue increase on this slide. Emphasize the 24% growth."
    );

    prs.save("with-notes.pptx", SaveFormat.PPTX);
}

Ví dụ Ghi chú Đơn giản hơn

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;

try (Presentation prs = new Presentation()) {
    var slide = prs.getSlides().get(0);
    slide.getShapes().addAutoShape(ShapeType.RECTANGLE, 100, 100, 500, 250)
         .addTextFrame("Main Content");

    var notes = slide.getNotesSlideManager().addNotesSlide();
    notes.getNotesTextFrame().setText("These are the speaker notes for this slide.");

    prs.save("notes.pptx", SaveFormat.PPTX);
}

Kiểm tra xem một slide ghi chú đã tồn tại chưa

getNotesSlideManager().getNotesSlide() trả về null nếu chưa có slide ghi chú nào được tạo.

import org.aspose.slides.foss.Presentation;

try (Presentation prs = new Presentation("existing.pptx")) {
    for (int i = 0; i < prs.getSlides().size(); i++) {
        var slide = prs.getSlides().get(i);
        var existingNotes = slide.getNotesSlideManager().getNotesSlide();
        if (existingNotes != null) {
            String text = existingNotes.getNotesTextFrame().getText();
            System.out.println("Slide " + (i + 1) + " notes: " + text);
        } else {
            System.out.println("Slide " + (i + 1) + ": no notes");
        }
    }
}

Thêm ghi chú cho nhiều slide

import org.aspose.slides.foss.Presentation;
import org.aspose.slides.foss.ShapeType;
import org.aspose.slides.foss.export.SaveFormat;

String[] noteTexts = {
    "Opening remarks: introduce the agenda.",
    "Key metrics: emphasize Q4 results.",
    "Closing: call to action.",
};

try (Presentation prs = new Presentation()) {
    // Add slides 2 and 3
    var layout = prs.getSlides().get(0).getLayoutSlide();
    prs.getSlides().addEmptySlide(layout);
    prs.getSlides().addEmptySlide(layout);

    for (int i = 0; i < prs.getSlides().size(); i++) {
        var slide = prs.getSlides().get(i);
        slide.getShapes().addAutoShape(ShapeType.RECTANGLE, 50, 50, 600, 300)
             .addTextFrame("Slide " + (i + 1));
        var n = slide.getNotesSlideManager().addNotesSlide();
        n.getNotesTextFrame().setText(noteTexts[i]);
    }

    prs.save("all-notes.pptx", SaveFormat.PPTX);
}

Xem Thêm

 Tiếng Việt