PDFへのテキスト注釈の使用方法(Python経由)
Contents
 [
      
        Hide
      ]
    既存のPDFファイルにテキスト注釈を追加する方法
テキスト注釈は、PDFドキュメント内の特定の場所に付けられた注釈です。閉じた状態では、注釈はアイコンとして表示され、開いた状態では、読者が選んだフォントとサイズでメモテキストを含むポップアップウィンドウとして表示されるべきです。
注釈は、特定のページのAnnotationsコレクションによって保持されます。このコレクションには、その個々のページのみの注釈が含まれています; 各ページには独自のAnnotationsコレクションがあります。
特定のページに注釈を追加するには、そのページのAnnotationsコレクションにadd()メソッドを使用して追加します。
- 
まず、PDFに追加したい注釈を作成します。 
- 
次に、入力PDFを開きます。 
- 
‘ページ’ オブジェクトの Annotations コレクションに注釈を追加します。 
次のコードスニペットは、PDFページに注釈を追加する方法を示しています。
    import aspose.pdf as ap
    document = ap.Document(input_file)
    textAnnotation = ap.annotations.TextAnnotation(
        document.pages[1], ap.Rectangle(300, 700.664, 320, 720.769, True)
    )
    textAnnotation.title = "Aspose User"
    textAnnotation.subject = "挿入されたテキスト 1"
    textAnnotation.contents = "qwerty"
    textAnnotation.flags = ap.annotations.AnnotationFlags.PRINT
    textAnnotation.color = ap.Color.blue
    document.pages[1].annotations.append(textAnnotation)
    document.save(output_file)
PDFファイルからテキスト注釈を取得
    import aspose.pdf as ap
    document = ap.Document(input_file)
    textAnnotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.TEXT)
    ]
    for ta in textAnnotations:
        print(ta.rect)
PDFファイルからテキスト注釈を削除する
    import aspose.pdf as ap
    document = ap.Document(input_file)
    textAnnotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.TEXT)
    ]
    for ta in textAnnotations:
        document.pages[1].annotations.delete(ta)
    document.save(output_file)
新しいフリーテキスト注釈を追加(または作成)する方法
フリーテキスト注釈は、ページ上に直接テキストを表示します。FreeTextAnnotationクラスは、このタイプの注釈を作成することを可能にします。以下のスニペットでは、文字列の最初の出現の上にフリーテキスト注釈を追加します。
    import aspose.pdf as ap
    # PDFファイルを読み込む
    document = ap.Document(input_file)
    freeTextAnnotation = ap.annotations.FreeTextAnnotation(
        document.pages[1], ap.Rectangle(299, 713, 308, 720, True), ap.annotations.DefaultAppearance()
    )
    freeTextAnnotation.title = "Aspose User"
    freeTextAnnotation.color = ap.Color.light_green
    document.pages[1].annotations.append(freeTextAnnotation)
    document.save(output_file)
PDFファイルからフリーテキスト注釈を取得する
    import aspose.pdf as ap
    document = ap.Document(input_file)
    freeTextAnnotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.FREE_TEXT)
    ]
    for fa in freeTextAnnotations:
        print(fa.rect)
PDFファイルからフリーテキスト注釈を削除する
    import aspose.pdf as ap
    # PDFファイルを読み込む
    document = ap.Document(input_file)
    freeTextAnnotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.FREE_TEXT)
    ]
    for fa in freeTextAnnotations:
        document.pages[1].annotations.delete(fa)
    document.save(output_file)
StrikeOutAnnotationを使用して単語を取り消し線で消す
Aspose.PDF for Pythonを使用すると、PDFドキュメントの注釈を追加、削除、および更新することができます。 あるクラスのひとつは注釈を取り消し線で引くこともできます。StrikeOutAnnotationがPDFに適用されると、指定されたテキストに線が引かれ、それが削除されるべきか無視されるべきであることを示します。
次のコードスニペットは、PDFにStrikeOutAnnotationを追加する方法を示しています。
    import aspose.pdf as ap
    document = ap.Document(input_file)
    strikeoutAnnotation = ap.annotations.StrikeOutAnnotation(
        document.pages[1], ap.Rectangle(299.988, 713.664, 308.708, 720.769, True)
    )
    strikeoutAnnotation.title = "Aspose User"
    strikeoutAnnotation.subject = "挿入されたテキスト 1"
    strikeoutAnnotation.flags = ap.annotations.AnnotationFlags.PRINT
    strikeoutAnnotation.color = ap.Color.blue
    document.pages[1].annotations.append(strikeoutAnnotation)
    document.save(output_file)
PDFからStrikeOutAnnotationを取得
    import aspose.pdf as ap
    document = ap.Document(input_file)
    StrikeoutAnnotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT)
    ]
    for pa in StrikeoutAnnotations:
        print(pa.rect)
PDFからStrikeOutAnnotationを削除
    import aspose.pdf as ap
    document = ap.Document(input_file)
    StrikeoutAnnotations = [
        a
        for a in document.pages[1].annotations
        if (a.annotation_type == ap.annotations.AnnotationType.STRIKE_OUT)
    ]
    for pa in StrikeoutAnnotations:
        document.pages[1].annotations.delete(pa)
    document.save(output_file)