1

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.")
Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
  • Ever considered PyMuPDF? You can define a link with an URL-target (or whatever else ...) with a "hot" area under which there is an empty space. Why would you want to have a button? – Jorj McKie Apr 21 '23 at 19:04

2 Answers2

0

No need to add a button with exploitable scripts, simply use ANY code or console PDF tool to watermark "https://www.google.be" wm.pdf
Use plain white or clear (when allowed) text to make it "invisible".

That simply places "plain text" on the page and just like in a web page, any such text is a URL to become a hyperlink.

enter image description here enter image description here

There is no physical coincidence between a Generated URL Hyperlink and a button both can be, or not be, on a page, and neither need to be visible, nor exist. There are many ways to generate scripts buttons and links but simplest of all is just
https://followed/by/qualified/address

enter image description here

You need to consider scale of text as it often can be a problem if line is broken. enter image description here

%PDF-1.7
%µ¶
1 0 obj <</Pages 2 0 R/Type/Catalog>> endobj
2 0 obj <</Count 1/Kids[3 0 R]/MediaBox[0 0 612 792]/Type/Pages>> endobj
3 0 obj <</Parent 2 0 R/Resources<</Font<</F1 4 0 R>>>>/Contents 5 0 R/Type/Page>> endobj
4 0 obj <</BaseFont/Helvetica/Subtype/Type1/Type/Font>> endobj
5 0 obj <</Length 317>>
stream
/GS0 gs 1 0 0 1 72 756 cm
BT 1 g 0 Tc 0 Tw 100 Tz 0 Tr/Helv 60 Tf 90 -116 Td (https://)Tj -90 -68 Td (www.google.be)Tj ET
/GS1 gs .75 .75 .75 rg 1 0 0 1 -72 -756 cm 250 725 72 20 re f
BT 0 g -720 TL/F1 12 Tf 268.654 730.764 Td (Button)Tj ET 
endstream
endobj

xref
0 6
0000000000 65536 f 
0000000015 00000 n 
0000000060 00000 n 
0000000133 00000 n 
0000000223 00000 n 
0000000286 00000 n 

trailer
<</Size 6/Root 1 0 R/ID[<61FEDF85EF4342CAD7BCEA2959EC0A6C><1BA0343CD64AAB92980321D74123DB39>]>>
startxref
578
%%EOF

to delete the button and see it still works remove the visible two lines i.e. the rect and word "button".

/GS1 gs .75 .75 .75 rg 1 0 0 1 -72 -756 cm 250 725 72 20 re f
BT 0 g -720 TL/F1 12 Tf 268.654 730.764 Td (Button)Tj ET 
K J
  • 8,045
  • 3
  • 14
  • 36
0

disclaimer I am the author of borb.

You are halfway there. You need to use RemoteGoToAnnotation which takes a bounding box (something of type Rectangle) and a uri (something of type str).

doc: typing.Optional[Document] = None
with open("input.pdf", "rb") as in_file_handle:
    doc = PDF.loads(in_file_handle)

# check whether we have read a Document
assert doc is not None

# add annotation
doc.get_page(0).add_annotation(RemoteGoToAnnotation(bounding_box=Rectangle(0, 0, 100, 100), uri="https://www.borbpdf.com"))

# store
with open("output.pdf", "wb") as output_file:
    PDF.dumps(output_file, doc)

Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54