在 Python 中添加和删除 PDF 书签
Contents
[
Hide
]
在 PDF 文档中添加书签
书签存放在 Document 对象的 OutlineItemCollection 集合本身在 OutlineCollection 集合。
要向 PDF 添加书签:
- 使用打开 PDF 文档 文档 对象。
- 创建书签并定义其属性。
- 添加 OutlineItemCollection 集合到 Outlines 集合。
下面的代码片段展示了如何在 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 文件添加子书签,首先添加父书签:
- 打开文档。
- 将书签添加到 OutlineItemCollection,定义其属性。
- 将 OutlineItemCollection 添加到 Document 对象的 OutlineCollection 集合。
子书签的创建方式与上文解释的父书签相同,但它被添加到父书签的 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 中的所有书签都存储在 OutlineCollection 集合中。本篇文章解释如何从 PDF 文件中删除所有书签。
要从 PDF 文件中删除所有书签:
- 调用 OutlineCollection collection 的 Delete 方法。
- 使用以下方式保存已修改的文件 文档 对象的 save() 方法。
以下代码片段展示了如何从 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 文件中删除特定书签,请:
- 将书签的标题作为参数传递给 OutlineCollection collection 的 Delete 方法。
- 然后使用 Document 对象的 Save 方法保存更新后的文件。
这 文档 class’ 提供了 OutlineCollection 集合。The delete() method 删除传递给该方法的标题的任何书签。
以下代码片段展示了如何从 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)