Anotasi keamanan menggunakan Java

Alur kerja anotasi keamanan dalam bagian ini fokus pada menyiapkan dan menerapkan redaksi pada konten PDF yang sensitif.

Menandai teks dengan anotasi redaksi

Gunakan contoh ini ketika teks yang cocok harus ditutupi oleh anotasi redaksi sebelum redaksi diterapkan secara permanen.

  1. Buka PDF sumber Document.
  2. Cari teks target dan buat sebuah RedactionAnnotation untuk setiap kecocokan.
  3. Konfigurasikan tampilan redaksi dan simpan dokumen.
public static void markTextRedaction(Path inputFile, Path outputFile, String searchTerm) {
    try (Document document = new Document(inputFile.toString())) {
        TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber(searchTerm);
        TextSearchOptions textSearchOptions = new TextSearchOptions(true);
        textFragmentAbsorber.setTextSearchOptions(textSearchOptions);
        document.getPages().accept(textFragmentAbsorber);

        for (var textFragment : textFragmentAbsorber.getTextFragments()) {
            Page page = textFragment.getPage();
            RedactionAnnotation redactionAnnotation = new RedactionAnnotation(page, textFragment.getRectangle());
            redactionAnnotation.setFillColor(Color.getGray());
            redactionAnnotation.setBorderColor(Color.getRed());
            redactionAnnotation.setColor(Color.getWhite());
            redactionAnnotation.setOverlayText("REDACTED");
            redactionAnnotation.setTextAlignment(HorizontalAlignment.Center);
            redactionAnnotation.setRepeat(true);
            page.getAnnotations().add(redactionAnnotation, true);
        }
        document.save(outputFile.toString());
    }
}

Menerapkan redaksi yang ada

Contoh ini secara permanen menerapkan anotasi redaksi yang sudah ada di halaman.

  1. Buka PDF sumber Document.
  2. Kumpulkan anotasi tipe AnnotationType.Redaction.
  3. Panggil redact() pada setiap anotasi yang dikumpulkan dan simpan file yang telah diperbarui.
public static void applyRedaction(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<RedactionAnnotation> redactionAnnotations = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Redaction) {
                redactionAnnotations.add((RedactionAnnotation) annotation);
            }
        }
        for (RedactionAnnotation redactionAnnotation : redactionAnnotations) {
            redactionAnnotation.redact();
        }
        document.save(outputFile.toString());
    }
}

Menyensor area halaman yang dipilih

Gunakan pendekatan ini ketika konten target diidentifikasi berdasarkan posisi daripada dengan mencocokkan teks.

  1. Buka PDF sumber Document.
  2. Deteksi persegi panjang target pada halaman, misalnya dari penempatan gambar.
  3. Buat sebuah RedactionAnnotation untuk area itu dan simpan dokumen.
public static void redactArea(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        ImagePlacementAbsorber imagePlacementAbsorber = new ImagePlacementAbsorber();
        Page page = document.getPages().get_Item(1);
        page.accept(imagePlacementAbsorber);

        com.aspose.pdf.Rectangle targetRect = imagePlacementAbsorber.getImagePlacements().get_Item(2).getRectangle();
        RedactionAnnotation redactionAnnotation = new RedactionAnnotation(page, targetRect);
        redactionAnnotation.setFillColor(Color.getGray());
        redactionAnnotation.setBorderColor(Color.getRed());
        redactionAnnotation.setColor(Color.getWhite());
        redactionAnnotation.setOverlayText("REDACTED");
        redactionAnnotation.setTextAlignment(HorizontalAlignment.Center);
        redactionAnnotation.setRepeat(true);

        page.getAnnotations().add(redactionAnnotation, true);
        document.save(outputFile.toString());
    }
}

Topik anotasi terkait