Add and Delete PDF Bookmarks in Python
Add a Bookmark to a PDF Document
Bookmarks are held in the Document object’s OutlineItemCollection collection, itself in the OutlineCollection collection.
To add a bookmark to a PDF:
- Open a PDF document using Document object.
- Create a bookmark and define its properties.
- Add the OutlineItemCollection collection to the Outlines collection.
The following code snippet shows you how to add a bookmark in a PDF document.
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)
Add a Child Bookmark to the PDF Document
Bookmarks can be nested, indicating a hierarchical relationship with parent and child bookmarks. This article explains how to add a child bookmark, that is, a second-level bookmark, to a PDF.
To add a child bookmark to a PDF file, first add a parent bookmark:
- Open a document.
- Add a bookmark to the OutlineItemCollection, defining its properties.
- Add the OutlineItemCollection to the Document object’s OutlineCollection collection.
The child bookmark is created just like the parent bookmark, explained above, but is added to the parent bookmark’s Outlines collection
The following code snippets show how to add child bookmark to a PDF document.
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)
Delete all Bookmarks from a PDF Document
All bookmarks in a PDF are held in the OutlineCollection collection. This article explains how to delete all bookmarks from a PDF file.
To delete all bookmarks from a PDF file:
- Call the OutlineCollection collection’s Delete method.
- Save the modified file using the Document object’s save() method.
The following code snippets show how to delete all bookmarks from a PDF document.
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)
Delete a Particular Bookmark from a PDF Document
To delete a particular bookmark from a PDF file:
- Pass the bookmark’s title as parameter to the OutlineCollection collection’s Delete method.
- Then save the updated file with the Document object Save method.
The Document class’ provides the OutlineCollection collection. The delete() method removes any bookmark with the title passed to the method.
The following code snippets show how to delete a particular bookmark from the PDF document.
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)