Anotações de segurança usando Java

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.

  1. Abra o PDF de origem em um objeto Document.
  2. Pesquise o texto alvo e crie um RedactionAnnotation para cada correspondência.
  3. 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.

  1. Abra o PDF de origem em um objeto Document.
  2. Colete anotações do tipo AnnotationType.Redaction.
  3. 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.

  1. Abra o PDF de origem em um objeto Document.
  2. Detecte o retângulo alvo na página, por exemplo a partir de uma colocação de imagem.
  3. 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());
    }
}

Tópicos de anotação relacionados