Add PDF Bookmarks with Named Destinations
Possible Usage Scenarios
Named Destinations are special kinds of bookmarks or links in PDF that do not depend on PDF pages. It means, if pages are added or deleted from PDF, bookmarks may become invalid but named destinations will remain intact. To create Named Destination, please set the PdfBookmarkEntry.destination_name property.
Add PDF Bookmarks with Named Destinations
Please see the following sample code, its source Excel file, and its output PDF file. The screenshot shows the bookmarks and named destinations inside the output PDF. The screenshot also describes how to view Named Destinations and that you need Professional version of Acrobat Reader.
Sample Code
from aspose.cells import PdfSaveOptions, Workbook | |
from aspose.cells.rendering import PdfBookmarkEntry | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Load source Excel file | |
wb = Workbook(sourceDir + "samplePdfBookmarkEntry_DestinationName.xlsx") | |
# Access first worksheet | |
ws = wb.worksheets[0] | |
# Access cell C5 | |
cell = ws.cells.get("C5") | |
# Create Bookmark and Destination for this cell | |
bookmarkEntry = PdfBookmarkEntry() | |
bookmarkEntry.text = "Text" | |
bookmarkEntry.destination = cell | |
bookmarkEntry.destination_name = "AsposeCells--" + cell.name | |
# Access cell G56 | |
cell = ws.cells.get("G56") | |
# Create Sub-Bookmark and Destination for this cell | |
subbookmarkEntry1 = PdfBookmarkEntry() | |
subbookmarkEntry1.text = "Text1" | |
subbookmarkEntry1.destination = cell | |
subbookmarkEntry1.destination_name = "AsposeCells--" + cell.name | |
# Access cell L4 | |
cell = ws.cells.get("L4") | |
# Create Sub-Bookmark and Destination for this cell | |
subbookmarkEntry2 = PdfBookmarkEntry() | |
subbookmarkEntry2.text = "Text2" | |
subbookmarkEntry2.destination = cell | |
subbookmarkEntry2.destination_name = "AsposeCells--" + cell.name | |
# Add Sub-Bookmarks in list | |
list = [] | |
list.append(subbookmarkEntry1) | |
list.append(subbookmarkEntry2) | |
# Assign Sub-Bookmarks list to Bookmark Sub-Entry | |
bookmarkEntry.sub_entry = list | |
# Create PdfSaveOptions and assign Bookmark to it | |
opts = PdfSaveOptions() | |
opts.bookmark = bookmarkEntry | |
# Save the workbook in Pdf format with given pdf save options | |
wb.save(outputDir + "outputPdfBookmarkEntry_DestinationName.pdf", opts) |