Annotations
このガイドでは、Java 用の Aspose.PDF FOSS における Annotation クラス階層を使用して PDF 注釈を追加、構成、削除する方法を示します。ウィジェット、フリーテキスト、ハイライト、サークル注釈の作成方法と、PdfAnnotationEditor を使用してそれらをフラット化または削除する方法を学びます。
注釈階層
抽象的な Annotation クラスは、すべての PDF 注釈の基礎です。すべての注釈は Page 上の Rectangle に配置され、page.getAnnotations().add(annotation) を通じて追加されます。
WidgetAnnotation
WidgetAnnotation はフォームウィジェットを表します。外観は AppearanceCharacteristics を通じて制御され、setBorder()、setBackground()、setRotate()、および setCaption() が公開されます。
try (Document doc = new Document()) {
PageCollection pages = doc.getPages();
Page page = pages.add();
WidgetAnnotation w = new WidgetAnnotation(page, new Rectangle(0, 0, 100, 50));
w.getCharacteristics().setBorder(Color.fromRgb(1, 0, 0));
w.getCharacteristics().setCaption("Submit");
page.getAnnotations().add(w);
doc.save("annotated.pdf");
}FreeTextAnnotation
FreeTextAnnotation は指定された位置にテキストコールアウトを表示します。フォント、サイズ、注釈テキストの色を設定するための DefaultAppearance を受け取ります。
DefaultAppearance da = new DefaultAppearance("Helv", 12, Color.BLACK);
FreeTextAnnotation fta = new FreeTextAnnotation(page,
new Rectangle(50, 50, 200, 100), da);
fta.setContents("Review comment here");
page.getAnnotations().add(fta);setCallout(double[][]) はアノテーションの三点呼び出し線を設定します。getIntent() はアノテーションの FreeTextIntent 値を返します。
マークアップアノテーション
HighlightAnnotation と UnderlineAnnotation はテキストスパンを四角形のポイントでマークします。getQuadPoints() メソッドは、マークされた領域の四隅を定義する8要素の配列を返します。
try (Document doc = new Document("input.pdf")) {
PageCollection pages = doc.getPages();
Page page = pages.get(1);
HighlightAnnotation h = new HighlightAnnotation(page,
new Rectangle(100, 200, 300, 220));
page.getAnnotations().add(h);
doc.save("highlighted.pdf");
}CircleAnnotation と CaretAnnotation
CircleAnnotation は指定された矩形に楕円を描画します;CaretAnnotation はテキスト内に挿入ポイントをマークします。両方とも Annotation を継承し、同じ色と枠線のプロパティを共有します。
try (Document doc = new Document()) {
PageCollection pages = doc.getPages();
Page page = pages.add();
CircleAnnotation circle = new CircleAnnotation(page, new Rectangle(100, 100, 200, 200));
circle.setColor(Color.fromRgb(0, 0, 1));
page.getAnnotations().add(circle);
doc.save("circle.pdf");
}アノテーションの平坦化
PdfAnnotationEditor.flattenAnnotations() を使用して、すべてのアノテーションをページコンテンツストリームにマージし、編集できないようにします。
try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
editor.bindPdf("annotated.pdf");
editor.flattenAnnotations();
editor.save("flat.pdf");
}タイプ別アノテーションの削除
バインドされたドキュメントのすべてのページで、注釈タイプ名を指定して deleteAnnotations(type) を呼び出し、そのタイプのすべての注釈を削除します:
try (PdfAnnotationEditor editor = new PdfAnnotationEditor()) {
editor.bindPdf("input.pdf");
editor.deleteAnnotations("Highlight");
editor.save("output.pdf");
}