Add PDF Bookmarks
This article provides information on how to insert PDF bookmarks when converting a spreadsheet to PDF.
Aspose.Cells for Python via .NET allows you to add bookmarks on the fly. PDF bookmarks can drastically improve the navigability of long documents. When adding bookmark links to PDF document, you can have precise control over the exact view you want, you’re not limited to linking to a page. You can set up the precise view by positioning the target page, and then create the bookmark.
Please see the following sample code to find out how to add PDF bookmarks. The code generates a simple workbook, specifies PDF bookmarks with destination locations and generates the PDF file.
from aspose.cells import PdfSaveOptions, Workbook | |
from aspose.cells.rendering import PdfBookmarkEntry | |
from os import os, path | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Create directory if it is not already present. | |
IsExists = path.isdir(dataDir) | |
if notIsExists: | |
os.makedirs(dataDir) | |
# Instantiate a new workbook | |
workbook = Workbook() | |
# Get the cells in the first(default) worksheet | |
cells = workbook.worksheets[0].cells | |
# Get the A1 cell | |
p = cells.get("A1") | |
# Enter a value | |
p.put_value("Preface") | |
# Get the A10 cell | |
A = cells.get("A10") | |
# Enter a value. | |
A.put_value("page1") | |
# Get the H15 cell | |
D = cells.get("H15") | |
# Enter a value | |
D.put_value("page1(H15)") | |
# Add a new worksheet to the workbook | |
workbook.worksheets.add() | |
# Get the cells in the second sheet | |
cells = workbook.worksheets[1].cells | |
# Get the B10 cell in the second sheet | |
B = cells.get("B10") | |
# Enter a value | |
B.put_value("page2") | |
# Add a new worksheet to the workbook | |
workbook.worksheets.add() | |
# Get the cells in the third sheet | |
cells = workbook.worksheets[2].cells | |
# Get the C10 cell in the third sheet | |
C = cells.get("C10") | |
# Enter a value | |
C.put_value("page3") | |
# Create a main PDF Bookmark entry object | |
pbeRoot = PdfBookmarkEntry() | |
# Specify its text | |
pbeRoot.text = "Sections" | |
# Set the destination cell/location | |
pbeRoot.destination = p | |
# Set its sub entry array list | |
pbeRoot.sub_entry = [] | |
# Create a sub PDF Bookmark entry object | |
subPbe1 = PdfBookmarkEntry() | |
# Specify its text | |
subPbe1.text = "Section 1" | |
# Set its destination cell | |
subPbe1.destination = A | |
# Define/Create a sub Bookmark entry object of "Section A" | |
ssubPbe = PdfBookmarkEntry() | |
# Specify its text | |
ssubPbe.text = "Section 1.1" | |
# Set its destination | |
ssubPbe.destination = D | |
# Create/Set its sub entry array list object | |
subPbe1.sub_entry = [] | |
# Add the object to "Section 1" | |
subPbe1.sub_entry.append(ssubPbe) | |
# Add the object to the main PDF root object | |
pbeRoot.sub_entry.append(subPbe1) | |
# Create a sub PDF Bookmark entry object | |
subPbe2 = PdfBookmarkEntry() | |
# Specify its text | |
subPbe2.text = "Section 2" | |
# Set its destination | |
subPbe2.destination = B | |
# Add the object to the main PDF root object | |
pbeRoot.sub_entry.append(subPbe2) | |
# Create a sub PDF Bookmark entry object | |
subPbe3 = PdfBookmarkEntry() | |
# Specify its text | |
subPbe3.text = "Section 3" | |
# Set its destination | |
subPbe3.destination = C | |
# Add the object to the main PDF root object | |
pbeRoot.sub_entry.append(subPbe3) | |
# Create an instance of PdfSaveOptions | |
pdfSaveOptions = PdfSaveOptions() | |
# Set the PDF Bookmark root object | |
pdfSaveOptions.bookmark = pbeRoot | |
dataDir = dataDir + "PDFBookmarks_test.out.pdf" | |
# Save the pdf file | |
workbook.save(dataDir, pdfSaveOptions) |