通过 Python 处理 PDF 文档中的书签

如何使用 Python 以编程方式操作 PDF 中的书签。

如何使用 Python 库在 PDF 文档中使用书签

为了在 PDF 文件中添加书签,我们将使用 Aspose.PDF for .NET API,这是一款适用于 python-net 平台的功能丰富、功能强大且易于使用的文档操作 API。打开 NuGet 软件包管理器,搜索 aspose.pdf 然后安装。您也可以在软件包管理器控制台中使用以下命令。

Python Package Manager Console

pip install aspose-pdf

通过 Python 使用书签的步骤


你需要 Aspose.PDF for .NET 才能在你的环境中试用代码。

1.使用文档对象打开 PDF 文档。 1.创建书签并定义其属性。 1.将 OutlineItemCollection 集合添加到 OutlineC 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)