Python での PDF ブックマークの追加と削除

PDF ドキュメントへのブックマークの追加

ブックマークは Document オブジェクトに保持されます アウトラインアイテムコレクション コレクション自体は アウトラインコレクション コレクション。

PDF にブックマークを追加するには:

  1. を使用して PDF ドキュメントを開く 文書 対象。
  2. ブックマークを作成し、そのプロパティを定義します。
  3. を追加 アウトラインアイテムコレクション コレクションをアウトラインコレクションに追加します。

次のコードスニペットは、PDF ドキュメントにブックマークを追加する方法を示しています。

import aspose.pdf as ap
import sys
from os import path

def add_bookmark(infile, outfile):
    # Open PDF document
    document = ap.Document(infile)

    # Create a bookmark object
    pdf_outline = ap.OutlineItemCollection(document.outlines)
    pdf_outline.title = "Test Outline"
    pdf_outline.italic = True
    pdf_outline.bold = True

    # Set the destination page number
    pdf_outline.action = ap.annotations.GoToAction(document.pages[1])

    # Add bookmark to the document's outline collection
    outlines = document.outlines
    outlines.append(pdf_outline)

    # Save PDF document
    document.save(outfile)

PDF ドキュメントに子ブックマークを追加

ブックマークはネストでき、親ブックマークと子ブックマークとの階層関係を示します。この記事では、子ブックマーク、つまりセカンドレベルのブックマークを PDF に追加する方法について説明します。

PDF ファイルに子ブックマークを追加するには、まず親ブックマークを追加します。

  1. 文書を開きます。
  2. にブックマークを追加 アウトラインアイテムコレクション、そのプロパティを定義します。
  3. OutlineItemコレクションをドキュメントオブジェクトに追加します アウトラインコレクション コレクション。

子ブックマークは、上で説明した親ブックマークと同じように作成されますが、親ブックマークの Outlines コレクションに追加されます。

次のコードスニペットは、PDF ドキュメントに子ブックマークを追加する方法を示しています。

import aspose.pdf as ap
import sys
from os import path

def add_child_bookmark(infile, outfile):
    # Open PDF document
    document = ap.Document(infile)

    # Create a parent bookmark object
    pdf_outline = ap.OutlineItemCollection(document.outlines)
    pdf_outline.title = "Parent Outline"
    pdf_outline.italic = True
    pdf_outline.bold = True

    # Create a child bookmark object
    pdf_child_outline = ap.OutlineItemCollection(document.outlines)
    pdf_child_outline.title = "Child Outline"
    pdf_child_outline.italic = True
    pdf_child_outline.bold = True

    # Add child bookmark to parent bookmark's collection
    pdf_outline.append(pdf_child_outline)

    # Add parent bookmark to the document's outline collection
    document.outlines.append(pdf_outline)

    # Save PDF document
    document.save(outfile)

PDF ドキュメントからすべてのブックマークを削除する

PDF 内のすべてのブックマークは、次の場所に保持されます。 アウトラインコレクション コレクション。この記事では、PDF ファイルからすべてのブックマークを削除する方法について説明します。

PDF ファイルからすべてのブックマークを削除するには:

  1. に電話してください アウトラインコレクション コレクションの Delete メソッド。
  2. を使用して変更したファイルを保存します 文書 オブジェクトの 保存 () 方法。

次のコードスニペットは、PDF ドキュメントからすべてのブックマークを削除する方法を示しています。

import aspose.pdf as ap
import sys
from os import path

def delete_bookmarks(infile, outfile):
    # Open PDF document
    document = ap.Document(infile)

    # Delete all bookmarks in the PDF document
    document.outlines.delete()

    # Save PDF document
    document.save(outfile)

PDF ドキュメントから特定のブックマークを削除する

PDF ファイルから特定のブックマークを削除するには:

  1. ブックマークのタイトルをパラメータとしてに渡します アウトラインコレクション コレクションの Delete メソッド。
  2. 次に、更新したファイルを Document オブジェクトの Save メソッドで保存します。

ザの 文書 「クラス」が提供するのは アウトラインコレクション コレクション。ザル 削除 () メソッドは、メソッドに渡されたタイトルのブックマークをすべて削除します。

次のコードスニペットは、PDF ドキュメントから特定のブックマークを削除する方法を示しています。

import aspose.pdf as ap
import sys
from os import path

def delete_bookmark(infile, outfile):
    # Open PDF document
    document = ap.Document(infile)

    # Delete a specific bookmark by title.
    # Note: If multiple bookmarks have the same title, only the first matching bookmark will be deleted.
    document.outlines.delete("Child Outline")

    # Save PDF document
    document.save(outfile)

関連ブックマークトピック