I'm scanning through different python libraries for days yet, I haven't got a working code. I am a beginner with Python code.
I'm trying to do the following:
- open a pdf file
- add a invisible button to a specific region
- add a url to this button.
- save this file to a local folder on pc.
I got a lot of info from: https://github.com/jorisschellekens/borb-examples
But I can't figure out how I get a button which opens a url in your browser when clicked. Maybe with the javascriptpushbutton?
Anybody any ideas?
Many thanks in advance, Pieter-Jan
import typing
from borb.pdf.canvas.geometry.rectangle import Rectangle
from borb.pdf import Document
from borb.pdf.page.page import Page
from borb.pdf import PDF
from borb.pdf.canvas.layout.annotation.remote_go_to_annotation import RemoteGoToAnnotation
# read the Document
doc: typing.Optional[Document] = None
with open("path.pdf", "rb") as in_file_handle:
doc = PDF.loads(in_file_handle)
# check whether we have read a Document
assert doc is not None
# Maak functie die url opent.
def open_url(page: Page, url: str, rect: Rectangle):
annotation = RemoteGoToAnnotation(rect, url)
page.add_annotation(annotation)
# Voeg een onzichtbare knop toe aan een specifieke regio
pagina: Page = doc.get_page(0) # Haal de eerste pagina uit het document
button_rect = Rectangle(64, 130, 146-64, 167-130) # Stel de afmetingen van de knop in: x1, y1, breedte, hoogte
link = "www.google.be"
#pagina.add_annotation(button_rect) # Voeg de knop toe aan de pagina
# Voeg de knop toe aan de pagina met de open_url functie
pagina.add_annotation(open_url(pagina,link,button_rect))
# Sla het bestand op op uw computer
with open("path.pdf", "wb") as output_file:
PDF.dumps(output_file, doc)
print("pdf is klaar.")