Anotasi keamanan menggunakan Java
Contents
[
Hide
]
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.
- Buka PDF sumber
Document. - Cari teks target dan buat sebuah
RedactionAnnotationuntuk setiap kecocokan. - 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.
- Buka PDF sumber
Document. - Kumpulkan anotasi tipe
AnnotationType.Redaction. - 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.
- Buka PDF sumber
Document. - Deteksi persegi panjang target pada halaman, misalnya dari penempatan gambar.
- Buat sebuah
RedactionAnnotationuntuk 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());
}
}