Java を使用したセキュリティ注釈
Contents
[
Hide
]
このセクションでは、セキュリティ注釈を使用して機密性の高い PDF コンテンツの墨消しを準備し、適用するワークフローを説明します。
墨消し注釈によるテキストのマーク
墨消しを恒久的に適用する前に、一致するテキストを墨消し注釈で覆う必要がある場合は、この例を使用してください。
- ソース PDF の Document を開いてください。
- 対象テキストを検索し、一致箇所ごとに RedactionAnnotation を作成してください。
- 墨消し注釈の外観を設定し、ドキュメントを保存してください。
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());
}
}
既存の墨消し注釈の適用
この例では、ページに既に存在する墨消し注釈を恒久的に適用します。
- ソース PDF の Document を開いてください。
- 型が AnnotationType.
Redactionの注釈を収集してください。 - 収集した各注釈で
redact()を呼び出し、更新したファイルを保存してください。
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());
}
}
選択したページ領域の墨消し
対象コンテンツがテキストの一致ではなく位置で特定される場合に、このアプローチを使用します。
- ソース PDF の Document を開いてください。
- ページ上の対象矩形を検出してください。たとえば、画像の配置から取得してください。
- 対象領域に RedactionAnnotation を作成し、ドキュメントを保存してください。
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());
}
}