Java を使用したマークアップ注釈

このセクションのマークアップ注釈のワークフローは、ノートスタイルのコメント、キャレットマーカー、およびグループ化された置換・レビューシナリオに焦点を当てています。

テキスト注釈の追加

ページにポップアップメタデータ付きの付箋スタイルのテキスト注釈を配置する必要がある場合は、この例を使用してください。

  1. ソース PDF の Document を開いてください。
  2. TextAnnotation オブジェクトを作成し、そのタイトル、内容、アイコン、ポップアップを構成してください。
  3. ページに注釈を追加し、ドキュメントを保存してください。
public static void textAnnotationAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextAnnotation textAnnotation = new TextAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(299.988, 613.664, 428.708, 680.769, true));
        textAnnotation.setTitle("Aspose User");
        textAnnotation.setSubject("Sticky Note");
        textAnnotation.setContents("This is a text annotation added by Aspose.PDF for Java");
        textAnnotation.setFlags(AnnotationFlags.Print);
        textAnnotation.setColor(Color.getBlue());
        textAnnotation.setIcon(TextIcon.Help);

        PopupAnnotation popup = new PopupAnnotation(
                document.getPages().get_Item(1),
                new Rectangle(428.708, 613.664, 528.708, 713.664, true));
        popup.setOpen(true);
        textAnnotation.setPopup(popup);

        document.getPages().get_Item(1).getAnnotations().add(textAnnotation, false);
        document.save(outputFile.toString());
    }
}

テキスト注釈の取得

この例では、ページをスキャンして各テキスト注釈の矩形を出力します。

  1. ソース PDF の Document を開いてください。
  2. ページ上の注釈を反復処理してください。
  3. AnnotationType.Text で注釈をフィルタリングし、それらの矩形を出力してください。
public static void textAnnotationGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Text) {
                System.out.println(annotation.getRect());
            }
        }
    }
}

テキスト注釈の削除

既存のテキスト注釈をドキュメントから削除する必要がある場合は、このアプローチを使用してください。

  1. ソース PDF の Document を開いてください。
  2. 型が AnnotationType.Text の注釈を収集してください。
  3. 収集された注釈を削除し、出力ファイルを保存してください。
public static void textAnnotationDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        List<Annotation> toDelete = new ArrayList<>();
        for (Annotation annotation : document.getPages().get_Item(1).getAnnotations()) {
            if (annotation.getAnnotationType() == AnnotationType.Text) {
                toDelete.add(annotation);
            }
        }
        for (Annotation annotation : toDelete) {
            document.getPages().get_Item(1).getAnnotations().delete(annotation);
        }
        document.save(outputFile.toString());
    }
}

キャレット注釈の追加

挿入されたテキストをキャレットスタイルのレビュー注釈でマークする必要がある場合は、この例を使用してください。

  1. ソース PDF の Document を開いてください。
  2. CaretAnnotation を作成し、そのポップアップと外観を設定してください。
  3. ページに注釈を追加し、ドキュメントを保存してください。
public static void caretAnnotationsAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        CaretAnnotation caretAnnotation = new CaretAnnotation(
                page,
                new Rectangle(299.988, 713.664, 308.708, 720.769, true));
        caretAnnotation.setTitle("Aspose User");
        caretAnnotation.setSubject("Inserted text 1");
        caretAnnotation.setFlags(AnnotationFlags.Print);
        caretAnnotation.setColor(Color.getBlue());
        caretAnnotation.setPopup(new PopupAnnotation(
                page,
                new Rectangle(310, 713, 410, 730, true)));
        page.getAnnotations().add(caretAnnotation);

        document.save(outputFile.toString());
    }
}

キャレット注釈の取得

この例は、既存のキャレット注釈を読み取り、それらの位置を出力します。

  1. ソース PDF の Document を開いてください。
  2. ページの注釈を反復処理してください。
  3. 注釈をフィルタリングし、AnnotationType.Caret に一致するものについて、それらの矩形を出力してください。
public static void caretAnnotationsGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.Caret) {
                System.out.println(annot.getRect());
            }
        }
    }
}

キャレット注釈の削除

ページからキャレット注釈を削除する必要がある場合は、このアプローチを使用してください。

  1. ソース PDF の Document を開いてください。
  2. AnnotationType.Caret である注釈を収集してください。
  3. 収集した注釈を削除し、出力ドキュメントを保存してください。
public static void caretAnnotationsDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        List<Annotation> caretAnnotations = new ArrayList<>();
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.Caret) {
                caretAnnotations.add(annot);
            }
        }
        for (Annotation annot : caretAnnotations) {
            page.getAnnotations().delete(annot);
        }
        document.save(outputFile.toString());
    }
}

グループ化された置換注釈の追加

この例は、置換スタイルのレビューコメントを表すために、キャレット注釈と取り消し線注釈を組み合わせています。

  1. ソース PDF の Document を開いてください。
  2. キャレット注釈と関連する StrikeOutAnnotation を作成してください。
  3. 注釈を setInReplyTo および setReplyType でリンクし、ドキュメントを保存してください。
public static void replaceAnnotationsAdd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        CaretAnnotation caretAnnotation = new CaretAnnotation(
                page,
                new Rectangle(361.246, 727.908, 370.081, 735.107, true));
        caretAnnotation.setFlags(AnnotationFlags.Print);
        caretAnnotation.setSubject("Inserted text 2");
        caretAnnotation.setTitle("Aspose User");
        caretAnnotation.setColor(Color.getBlue());
        caretAnnotation.setPopup(new PopupAnnotation(
                page,
                new Rectangle(310, 713, 410, 730, true)));

        StrikeOutAnnotation strikeoutAnnotation = new StrikeOutAnnotation(
                page,
                new Rectangle(318.407, 727.826, 368.916, 740.098, true));
        strikeoutAnnotation.setColor(Color.getBlue());
        strikeoutAnnotation.setQuadPoints(new Point[]{
                new Point(321.66, 739.416),
                new Point(365.664, 739.416),
                new Point(321.66, 728.508),
                new Point(365.664, 728.508)
        });
        strikeoutAnnotation.setSubject("Cross-out");
        strikeoutAnnotation.setInReplyTo(caretAnnotation);
        strikeoutAnnotation.setReplyType(ReplyType.Group);

        page.getAnnotations().add(caretAnnotation);
        page.getAnnotations().add(strikeoutAnnotation);

        document.save(outputFile.toString());
    }
}

グループ化された置換注釈の取得

この例は、グループ化された置換ワークフローに参加する取り消し線注釈を検出します。

  1. ソース PDF の Document を開いてください。
  2. ページの注釈を繰り返し処理し、取り消し線注釈を選択してください。
  3. 返信関係を確認し、一致する注釈の矩形を出力してください。
public static void replaceAnnotationsGet(Path inputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.StrikeOut) {
                StrikeOutAnnotation sa = (StrikeOutAnnotation) annot;
                if (sa.getInReplyTo() != null && sa.getReplyType() == ReplyType.Group) {
                    System.out.println("Replace annotation rect: " + sa.getRect());
                }
            }
        }
    }
}

グループ化された置換アノテーションの削除

ページから置換レビューの取り消し線アノテーションを削除する必要がある場合は、このアプローチを使用してください。

  1. ソース PDF の Document を開いてください。
  2. 置換マークアップを表す取り消し線アノテーションを収集してください。
  3. 収集された注釈を削除し、更新された文書を保存してください。
public static void replaceAnnotationsDelete(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        Page page = document.getPages().get_Item(1);

        List<StrikeOutAnnotation> replaceAnnotations = new ArrayList<>();
        for (Annotation annot : page.getAnnotations()) {
            if (annot.getAnnotationType() == AnnotationType.StrikeOut) {
                replaceAnnotations.add((StrikeOutAnnotation) annot);
            }
        }
        for (StrikeOutAnnotation annot : replaceAnnotations) {
            page.getAnnotations().delete(annot);
        }
        document.save(outputFile.toString());
    }
}

関連する注釈トピック