Python 経由で PDF ドキュメント内のブックマークを操作する

Python を使ってPDFのブックマークをプログラム的に操作する方法。

Python ライブラリで PDF ドキュメント内のブックマークを操作する方法

PDFファイルにブックマークを追加するには、Aspose.PDF for .NET APIを使用します。このAPIは、python-net プラットフォーム用の機能豊富で強力で使いやすいドキュメント操作APIです。NuGet パッケージマネージャーを開き、Aspose.pdf を検索してインストールします。Package Manager コンソールから次のコマンドを使用することもできます。

Python Package Manager Console

pip install aspose-pdf

Python 経由でブックマークを操作する手順


お使いの環境でコードを試すには Aspose.PDF for .NET が必要です。

1。Document オブジェクトを使用して PDF ドキュメントを開きます。 1。ブックマークを作成し、そのプロパティを定義します。 1。OutlineItemCollection コレクションをアウトラインコレクションに追加します。 1。ファイルをもう一度保存する

<% bookmarks.code-block.text %>

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

<% bookmarks.code-block.subtitle %>

    def bookmark_add(self, infile, outfile):

        path_infile = self.dataDir + infile
        path_outfile = self.dataDir + outfile

        # Open document
        pdfDocument = Document(path_infile)

        # Create a bookmark object
        pdfOutline = OutlineItemCollection(pdfDocument.Outlines)
        pdfOutline.Title = "Test Outline"
        pdfOutline.Italic = True
        pdfOutline.Bold = True
        # Set the destination page number
        pdfOutline.Action = GoToAction(pdfDocument.Pages[1])
        # Add bookmark in the document's outline collection.
        pdfDocument.Outlines.Add(pdfOutline)

        # Save new output
        pdfDocument.Save(path_outfile)