Anotasi markup menggunakan Java

Alur kerja anotasi markup dalam bagian ini berfokus pada komentar bergaya catatan, penanda caret, dan skenario penggantian‑ulasan yang dikelompokkan.

Menambahkan anotasi teks

Gunakan contoh ini ketika Anda perlu menempatkan anotasi teks gaya catatan tempel dengan metadata popup pada sebuah halaman.

  1. Buka PDF sumber Document.
  2. Buat sebuah TextAnnotation dan mengatur judul, konten, ikon, dan popup-nya.
  3. Tambahkan anotasi ke halaman dan simpan dokumen.
public static void textAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextAnnotation textAnnotation = new TextAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 613.664, 428.708, 680.769, true));
        textAnnotation.setTitle("Aspose User");
        textAnnotation.setSubject("Sticky Note");
        textAnnotation.setContents("This is a text annotation added by Aspose.PDF for Java");
        textAnnotation.setFlags(AnnotationFlags.Print);
        textAnnotation.setColor(Color.getBlue());
        textAnnotation.setIcon(TextIcon.Help);

        PopupAnnotation popup = new PopupAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(428.708, 613.664, 528.708, 713.664, true));
        popup.setOpen(true);
        textAnnotation.setPopup(popup);

        document.getPages().get_Item(1).getAnnotations().add(textAnnotation, false);
        document.save(outputFile.toString());
    }
}

Mendapatkan anotasi teks

Contoh ini memindai halaman dan mencetak persegi panjang setiap anotasi teks.

  1. Buka PDF sumber Document.
  2. Iterasikan melalui anotasi pada halaman.
  3. Filter anotasi berdasarkan AnnotationType.Text dan cetak persegi panjang mereka.
public static void textAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Text) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

Menghapus anotasi teks

Gunakan pendekatan ini ketika anotasi teks yang ada harus dihapus dari dokumen.

  1. Buka PDF sumber Document.
  2. Kumpulkan anotasi tipe AnnotationType.Text.
  3. Hapus anotasi yang dikumpulkan dan simpan file output.
public static void textAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Text) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

Menambahkan anotasi caret

Gunakan contoh ini saat Anda perlu menandai teks yang disisipkan dengan anotasi tinjauan bergaya caret.

  1. Buka PDF sumber Document.
  2. Buat sebuah CaretAnnotation dan atur popup serta tampilannya.
  3. Tambahkan anotasi ke halaman dan simpan dokumen.
public static void caretAnnotationsAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        CaretAnnotation caretAnnotation = new CaretAnnotation(
                page,
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        caretAnnotation.setTitle("Aspose User");
        caretAnnotation.setSubject("Inserted text 1");
        caretAnnotation.setFlags(AnnotationFlags.Print);
        caretAnnotation.setColor(Color.getBlue());
        caretAnnotation.setPopup(new PopupAnnotation(
                page,
                new Rectangle(310, 713, 410, 730, true)));
        page.getAnnotations().add(caretAnnotation);

        document.save(outputFile.toString());
    }
}

Mendapatkan anotasi caret

Contoh ini membaca anotasi caret yang ada dan mencetak lokasinya.

  1. Buka PDF sumber Document.
  2. Iterasikan anotasi halaman.
  3. Filter anotasi berdasarkan AnnotationType.Caret dan cetak persegi panjang mereka.
public static void caretAnnotationsGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.Caret) {
                System.out.println(annot.getRect());
            }
        }
    }
}

Menghapus anotasi caret

Gunakan pendekatan ini ketika anotasi caret harus dihapus dari halaman.

  1. Buka PDF sumber Document.
  2. Kumpulkan anotasi yang tipenya adalah AnnotationType.Caret.
  3. Hapus anotasi yang dikumpulkan dan simpan dokumen keluaran.
public static void caretAnnotationsDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        List<Annotation> caretAnnotations = new ArrayList<>();
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.Caret) {
                caretAnnotations.add(annot);
            }
        }
        for (Annotation annot : caretAnnotations) {
            page.getAnnotations().delete(annot);
        }
        document.save(outputFile.toString());
    }
}

Menambahkan anotasi pengganti berkelompok

Contoh ini menggabungkan anotasi caret dengan anotasi coret untuk mewakili komentar tinjauan bergaya penggantian.

  1. Buka PDF sumber Document.
  2. Buat anotasi caret dan yang terkait StrikeOutAnnotation.
  3. Hubungkan anotasi melalui setInReplyTo dan setReplyType, lalu simpan dokumen.
public static void replaceAnnotationsAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        CaretAnnotation caretAnnotation = new CaretAnnotation(
                page,
                new Rectangle(361.246, 727.908, 370.081, 735.107, true));
        caretAnnotation.setFlags(AnnotationFlags.Print);
        caretAnnotation.setSubject("Inserted text 2");
        caretAnnotation.setTitle("Aspose User");
        caretAnnotation.setColor(Color.getBlue());
        caretAnnotation.setPopup(new PopupAnnotation(
                page,
                new Rectangle(310, 713, 410, 730, true)));

        StrikeOutAnnotation strikeoutAnnotation = new StrikeOutAnnotation(
                page,
                new Rectangle(318.407, 727.826, 368.916, 740.098, true));
        strikeoutAnnotation.setColor(Color.getBlue());
        strikeoutAnnotation.setQuadPoints(new Point[]{
                new Point(321.66, 739.416),
                new Point(365.664, 739.416),
                new Point(321.66, 728.508),
                new Point(365.664, 728.508)
        });
        strikeoutAnnotation.setSubject("Cross-out");
        strikeoutAnnotation.setInReplyTo(caretAnnotation);
        strikeoutAnnotation.setReplyType(ReplyType.Group);

        page.getAnnotations().add(caretAnnotation);
        page.getAnnotations().add(strikeoutAnnotation);

        document.save(outputFile.toString());
    }
}

Mendapatkan anotasi pengganti yang dikelompokkan

Contoh ini mendeteksi anotasi coret yang berpartisipasi dalam alur kerja penggantian berkelompok.

  1. Buka PDF sumber Document.
  2. Iterasikan anotasi halaman dan pilih anotasi coret.
  3. Periksa hubungan balasan dan cetak persegi panjang anotasi yang cocok.
public static void replaceAnnotationsGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.StrikeOut) {
                StrikeOutAnnotation sa = (StrikeOutAnnotation) annot;
                if (sa.getInReplyTo() != null && sa.getReplyType() == ReplyType.Group) {
                    System.out.println("Replace annotation rect: " + sa.getRect());
                }
            }
        }
    }
}

Menghapus anotasi pengganti yang dikelompokkan

Gunakan pendekatan ini ketika anotasi coret replace-review harus dihapus dari halaman.

  1. Buka PDF sumber Document.
  2. Kumpulkan anotasi coret yang mewakili markup pengganti.
  3. Hapus anotasi yang dikumpulkan dan simpan dokumen yang diperbarui.
public static void replaceAnnotationsDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        List<StrikeOutAnnotation> replaceAnnotations = new ArrayList<>();
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.StrikeOut) {
                replaceAnnotations.add((StrikeOutAnnotation) annot);
            }
        }
        for (StrikeOutAnnotation annot : replaceAnnotations) {
            page.getAnnotations().delete(annot);
        }
        document.save(outputFile.toString());
    }
}

Topik anotasi terkait