Bookmark in pdf

Sudic29
New Contributor

I am creating a pdf using pyspark and trying to make bookmarks for each table in the pages. All the bookmarks end up pointing to the first table in the first page. Please help me out here.

VZLA
Databricks Employee
Databricks Employee

@Sudic29 can you please share more about what you have implemented so far?

This requires dynamically tracking the page number during the PDF creation process. Example in python:

 

from PyPDF2 import PdfReader, PdfWriter

def add_bookmarks_to_pdf(input_path, output_path, bookmarks):
    # Open the PDF file
    reader = PdfReader(input_path)
    writer = PdfWriter()

    # Add pages to the writer and create bookmarks
    for i, page in enumerate(reader.pages):
        writer.add_page(page)
        if i in bookmarks:
            writer.add_named_destination(bookmarks[i], i)

    # Write the output PDF with bookmarks
    with open(output_path, 'wb') as f:
        writer.write(f)

# Example bookmarks: {page_number: "Bookmark Name"}
bookmarks = {
    0: "Table 1",
    1: "Table 2",
    2: "Table 3"
}
add_bookmarks_to_pdf("input.pdf", "output.pdf", bookmarks)

 

What have you accomplished in PySpark?