Работа с закладками в PDF-документе через Python

Как программно манипулировать закладками в PDF с помощью Python.

Как работать с закладками в PDF-документе с библиотекой Python

Чтобы добавить закладки в PDF-файл, мы будем использовать API Aspose.PDF for .NET, который представляет собой многофункциональный, мощный и простой в использовании API для работы с документами для платформы python-net. Откройте диспетчер пакетов NuGet, найдите Aspose.pdf и установите. Вы также можете использовать следующую команду из консоли Package Manager.

Python Package Manager Console

pip install aspose-pdf

Шаги по работе с закладками через Python


Вам нужно Aspose.PDF for .NET попробовать код в своей среде.

  1. Откройте PDF-документ с помощью объекта Document.
  2. Создайте закладку и определите ее свойства.
  3. Добавьте коллекцию OutlineItemCollection в коллекцию OutlineItemCollection.
  4. Сохраните файл снова

<% 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)