Annotations de sécurité utilisant Java
Contents
[
Hide
]
Les flux de travail d’annotation de sécurité de cette section se concentrent sur la préparation et l’application de suppressions au contenu PDF sensible.
Marquer le texte avec des annotations de caviardage
Utilisez cet exemple lorsque le texte correspondant doit être couvert par des annotations de caviardage avant que le caviardage soit appliqué de manière permanente.
- Ouvrez le PDF source Document.
- Recherchez le texte cible et créez une RedactionAnnotation pour chaque correspondance.
- Configurez l’apparence du caviardage et enregistrez le document.
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());
}
}
Appliquer les caviardages existantes
Cet exemple applique de manière permanente les annotations de caviardage qui existent déjà sur la page.
- Ouvrez le PDF source Document.
- Collectez les annotations de type AnnotationType.
Redaction. - Appelez
redact()sur chaque annotation collectée et enregistrez le fichier mis à jour.
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());
}
}
Caviarder une zone de page sélectionnée
Utilisez cette approche lorsque le contenu cible est identifié par position plutôt que par correspondance de texte.
- Ouvrez le PDF source Document.
- Détectez le rectangle cible sur la page, par exemple à partir d’un placement d’image.
- Créez une RedactionAnnotation pour cette zone et enregistrez le document.
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());
}
}