Add and Delete a Bookmark using 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
# Open document
document = ap.Document(input_pdf)
# Create a bookmark object
outline = ap.OutlineItemCollection(document.outlines)
outline.title = "Test Bookmark"
outline.italic = True
outline.bold = True
# Set the destination page number
outline.action = ap.annotations.GoToAction(document.pages[1])
# Add bookmark in the document's outline collection.
document.outlines.append(outline)
# Save output
document.save(output_pdf)
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
# Open document
document = ap.Document(input_pdf)
# Create a parent bookmark object
outline = ap.OutlineItemCollection(document.outlines)
outline.title = "Parent Outline"
outline.italic = True
outline.bold = True
# Create a child bookmark object
childOutline = ap.OutlineItemCollection(document.outlines)
childOutline.title = "Child Outline"
childOutline.italic = True
childOutline.bold = True
# Add child bookmark in parent bookmark's collection
outline.append(childOutline)
# Add parent bookmark in the document's outline collection.
document.outlines.append(outline)
# Save output
document.save(output_pdf)
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
# Open document
document = ap.Document(input_pdf)
# Delete all bookmarks
document.outlines.delete()
# Save updated file
document.save(output_pdf)
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
# Open document
document = ap.Document(input_pdf)
# Delete particular outline by Title
document.outlines.delete("Child Outline")
# Save updated file
document.save(output_pdf)