Python を使用したテキストベースのアノテーション
この記事では、.NET 経由で Aspose.PDF for Python を使用して PDF ドキュメント内のテキストベースの注釈を操作する方法を説明します。
このサンプルスクリプトは、いくつかのテキスト注釈ワークフローを示しています。
- フリーテキスト注釈
- ハイライト注釈
- 注釈に下線を引く
- 曲がりくねった注釈
- 取り消し線注釈
フリーテキスト注釈
フリーテキスト注釈の追加
フリーテキスト注釈を使用すると、表示されているテキストコメントを PDF ページに直接配置できます。この例では、最初のページに簡単なフリーテキスト注釈を追加します。
ソース PDF を開く
document = ap.Document(infile)
フリーテキスト注釈の作成と設定
free_text_annotation = ap.annotations.FreeTextAnnotation(
document.pages[1],
ap.Rectangle(299, 713, 308, 720, True),
ap.annotations.DefaultAppearance(),
)
free_text_annotation.title = "Aspose User"
free_text_annotation.color = ap.Color.light_green
注釈を追加して PDF を保存する
document.pages[1].annotations.append(free_text_annotation)
document.save(outfile)
完全な例
def free_text_annotation_add(infile, outfile):
document = ap.Document(infile)
free_text_annotation = ap.annotations.FreeTextAnnotation(
document.pages[1],
ap.Rectangle(299, 713, 308, 720, True),
ap.annotations.DefaultAppearance(),
)
free_text_annotation.title = "Aspose User"
free_text_annotation.color = ap.Color.light_green
document.pages[1].annotations.append(free_text_annotation)
document.save(outfile)
フリーテキスト注釈を取得
フリーテキスト注釈を検査するには、最初のページの注釈を次の条件でフィルタリングします。 FREE_TEXT 各注釈長方形を入力して印刷します。
文書を読み込んでフリーテキスト注釈を集める
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
注記長方形を印刷
for annotation in free_text_annotations:
print(annotation.rect)
完全な例
def free_text_annotation_get(infile, outfile):
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
for annotation in free_text_annotations:
print(annotation.rect)
フリーテキスト注釈を削除
このワークフローは、最初のページからすべてのフリーテキスト注釈を削除し、更新された PDF を保存します。
フリーテキスト注釈の検索と削除
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
for annotation in free_text_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
完全な例
def free_text_annotation_delete(infile, outfile):
document = ap.Document(infile)
free_text_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.FREE_TEXT
]
for annotation in free_text_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
テキストマークアップ注釈
ハイライト注釈
テキストハイライトを追加
ハイライト注釈は、基になる内容を変更せずに文書の一部を強調します。次の使用例は、最初のページにハイライト注釈を追加します。
document = ap.Document(infile)
highlight_annotation = ap.annotations.HighlightAnnotation(
document.pages[1],
ap.Rectangle(300, 750, 320, 770, True),
)
document.pages[1].annotations.append(highlight_annotation)
document.save(outfile)
def text_highlight_annotation_add(infile, outfile):
document = ap.Document(infile)
highlight_annotation = ap.annotations.HighlightAnnotation(
document.pages[1],
ap.Rectangle(300, 750, 320, 770, True),
)
document.pages[1].annotations.append(highlight_annotation)
document.save(outfile)
テキストハイライトを取得
ハイライト注釈を確認するには、次の条件でページ注釈をフィルタリングします。 HIGHLIGHT 長方形を入力して印刷します。
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
print(annotation.rect)
def text_highlight_annotation_get(infile, outfile):
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
print(annotation.rect)
テキストハイライトを削除
このワークフローは、最初のページからすべてのハイライト注釈を削除し、出力 PDF を保存します。
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_highlight_annotation_delete(infile, outfile):
document = ap.Document(infile)
highlight_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.HIGHLIGHT
]
for annotation in highlight_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
注釈に下線を引く
テキスト下線注釈を追加
下線注釈は、テキストに下線が見えるようにマークします。この例では、基本的な下線注釈を追加し、そのメタデータと色を設定します。
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline 1"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
def text_underline_annotation_add(infile, outfile):
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline 1"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
テキスト下線注釈を追加して平坦化
下線をインタラクティブな注釈のままにするのではなく、ページコンテンツの一部にしたい場合は、追加後に下線をフラット化できます。
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline to Flatten"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
underline_annotation.flatten()
document.save(outfile)
def text_underline_flatten_add(infile, outfile):
document = ap.Document(infile)
underline_annotation = ap.annotations.UnderlineAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline to Flatten"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
document.pages[1].annotations.append(underline_annotation)
underline_annotation.flatten()
document.save(outfile)
四角点を使ったテキスト下線注釈の追加
四角点を使用すると、下線注釈のマーク領域を正確に定義できます。これは単純な四角形よりも詳細な制御が必要な場合に便利です。
document = ap.Document(infile)
rect = ap.Rectangle(299.988, 713.664, 308.708, 720.769, True)
underline_annotation = ap.annotations.UnderlineAnnotation(document.pages[1], rect)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline with Quad Points"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
underline_annotation.quad_points = [
ap.Point(rect.llx, rect.lly),
ap.Point(rect.urx, rect.lly),
ap.Point(rect.urx, rect.ury),
ap.Point(rect.llx, rect.ury),
]
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
def text_underline_with_quad_points_add(infile, outfile):
document = ap.Document(infile)
rect = ap.Rectangle(299.988, 713.664, 308.708, 720.769, True)
underline_annotation = ap.annotations.UnderlineAnnotation(document.pages[1], rect)
underline_annotation.title = "Aspose User"
underline_annotation.subject = "Inserted Underline with Quad Points"
underline_annotation.flags = ap.annotations.AnnotationFlags.PRINT
underline_annotation.color = ap.Color.blue
underline_annotation.quad_points = [
ap.Point(rect.llx, rect.lly),
ap.Point(rect.urx, rect.lly),
ap.Point(rect.urx, rect.ury),
ap.Point(rect.llx, rect.ury),
]
document.pages[1].annotations.append(underline_annotation)
document.save(outfile)
テキスト下線注釈を削除
このワークフローは、最初のページからすべての下線注釈を削除し、更新された文書を保存します。
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_underline_annotation_delete(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
タイトル別にテキスト下線注釈を削除
このワークフローは、タイトルを確認した後に下線注釈を選択的に削除する方法を示しています。
document = ap.Document(infile)
underline_annotations = [
cast(ap.annotations.UnderlineAnnotation, annotation)
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
if annotation.title.startswith("a"):
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_underline_by_title_delete(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
cast(ap.annotations.UnderlineAnnotation, annotation)
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
if annotation.title.startswith("a"):
document.pages[1].annotations.delete(annotation)
document.save(outfile)
テキスト下線注釈を取得
下線の注釈を確認するには、最初のページの注釈を次の条件でフィルタリングします。 UNDERLINE 各長方形を入力して印刷します。
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
print(annotation.rect)
def text_underline_annotation_get(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
print(annotation.rect)
テキスト下線注釈マーク付きテキストを取得
このワークフローは、各下線注釈を次のように変換します。 UnderlineAnnotation オブジェクトを作成し、マークされたテキストを抽出します。
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
print(f"Marked text: {ua.get_marked_text()}")
def text_underline_marked_text_get(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
print(f"Marked text: {ua.get_marked_text()}")
テキスト下線注釈とマークされたフラグメントを取得
マークされた各フラグメントが別々に必要な場合は、によって返されたコレクションを繰り返し処理できます。 get_marked_text_fragments().
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
for fragment in ua.get_marked_text_fragments():
print(f"Fragment text: {fragment.text}")
def text_underline_marked_fragments_get(infile, outfile):
document = ap.Document(infile)
underline_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.UNDERLINE
]
for annotation in underline_annotations:
ua = cast(ap.annotations.UnderlineAnnotation, annotation)
for fragment in ua.get_marked_text_fragments():
print(f"Fragment text: {fragment.text}")
曲がりくねった注釈
曲がりくねった注釈を追加
曲がりくねった注釈は、テキスト内のスペル、文法、注意領域を示すためによく使用されます。この例では、最初のページに曲がりくねった注釈を追加します。
document = ap.Document(infile)
page = document.pages[1]
squiggly_annotation = ap.annotations.SquigglyAnnotation(
page,
ap.Rectangle(67, 317, 261, 459, True),
)
squiggly_annotation.title = "John Smith"
squiggly_annotation.color = ap.Color.blue
page.annotations.append(squiggly_annotation)
document.save(outfile)
def text_squiggly_annotation_add(infile, outfile):
document = ap.Document(infile)
page = document.pages[1]
squiggly_annotation = ap.annotations.SquigglyAnnotation(
page,
ap.Rectangle(67, 317, 261, 459, True),
)
squiggly_annotation.title = "John Smith"
squiggly_annotation.color = ap.Color.blue
page.annotations.append(squiggly_annotation)
document.save(outfile)
曲がりくねった注釈を取得
曲がりくねった注釈を検査するには、次の条件でページ注釈をフィルタリングします。 SQUIGGLY 長方形を入力して印刷します。
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
print(annotation.rect)
def text_squiggly_annotation_get(infile, outfile):
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
print(annotation.rect)
曲がりくねった注釈を削除
このワークフローは、最初のページから波打った注釈をすべて削除し、結果を保存します。
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_squiggly_annotation_delete(infile, outfile):
document = ap.Document(infile)
squiggly_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.SQUIGGLY
]
for annotation in squiggly_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
取り消し線注釈
テキスト取り消し線注釈の追加
取り消し線付きの注釈は、削除または取り消し線を引いたものとして扱うべきテキストを示します。この例では、取り消し線付きの注釈を追加し、そのメタデータと色を設定します。
document = ap.Document(infile)
strikeout_annotation = ap.annotations.StrikeOutAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
strikeout_annotation.title = "Aspose User"
strikeout_annotation.subject = "Inserted text 1"
strikeout_annotation.flags = ap.annotations.AnnotationFlags.PRINT
strikeout_annotation.color = ap.Color.blue
document.pages[1].annotations.append(strikeout_annotation)
document.save(outfile)
def text_strikeout_annotation_add(infile, outfile):
document = ap.Document(infile)
strikeout_annotation = ap.annotations.StrikeOutAnnotation(
document.pages[1],
ap.Rectangle(299.988, 713.664, 308.708, 720.769, True),
)
strikeout_annotation.title = "Aspose User"
strikeout_annotation.subject = "Inserted text 1"
strikeout_annotation.flags = ap.annotations.AnnotationFlags.PRINT
strikeout_annotation.color = ap.Color.blue
document.pages[1].annotations.append(strikeout_annotation)
document.save(outfile)
テキスト取り消し線注釈を取得
取り消し線付きの注釈を確認するには、次の条件でページ注釈をフィルタリングします。 STRIKE_OUT 長方形を入力して印刷します。
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
print(annotation.rect)
def text_strikeout_annotation_get(infile, outfile):
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
print(annotation.rect)
テキスト取り消し線注釈の削除
このワークフローは、最初のページからすべての取り消し線付き注釈を削除し、更新された文書を保存します。
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)
def text_strikeout_annotation_delete(infile, outfile):
document = ap.Document(infile)
strikeout_annotations = [
annotation
for annotation in document.pages[1].annotations
if annotation.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT
]
for annotation in strikeout_annotations:
document.pages[1].annotations.delete(annotation)
document.save(outfile)