Anotações de segurança usando Java
Contents
[
Hide
]
Os fluxos de trabalho de anotação de segurança nesta seção focam na preparação e aplicação de redações ao conteúdo PDF sensível.
Marcar texto com anotações de redação
Use este exemplo quando o texto correspondente deve ser coberto por anotações de redação antes que a redação seja aplicada permanentemente.
- Abra o PDF de origem em um objeto Document.
- Pesquise o texto alvo e crie um RedactionAnnotation para cada correspondência.
- Configure a aparência da redação e salve o documento.
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());
}
}
Aplicar redações existentes
Este exemplo aplica permanentemente anotações de redação que já existem na página.
- Abra o PDF de origem em um objeto Document.
- Colete anotações do tipo AnnotationType.
Redaction. - Chame
redact()em cada anotação coletada e salve o arquivo atualizado.
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());
}
}
Redigir uma área de página selecionada
Use esta abordagem quando o conteúdo alvo for identificado por posição em vez de por correspondência de texto.
- Abra o PDF de origem em um objeto Document.
- Detecte o retângulo alvo na página, por exemplo a partir de uma colocação de imagem.
- Crie um RedactionAnnotation para essa área e salvar o documento.
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());
}
}