Security Annotations using Java
Contents
[
Hide
]
Security annotation workflows in this section focus on preparing and applying redactions to sensitive PDF content.
Mark text with redaction annotations
Use this example when matching text should be covered by redaction annotations before the redaction is permanently applied.
- Open the source PDF Document.
- Search for the target text and create a RedactionAnnotation for each match.
- Configure the redaction appearance and save the 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());
}
}
Apply existing redactions
This example permanently applies redaction annotations that already exist on the page.
- Open the source PDF Document.
- Collect annotations of type AnnotationType.
Redaction. - Call
redact()on each collected annotation and save the updated file.
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());
}
}
Redact a selected page area
Use this approach when the target content is identified by position rather than by matching text.
- Open the source PDF Document.
- Detect the target rectangle on the page, for example from an image placement.
- Create a RedactionAnnotation for that area and save the 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());
}
}