how to use insert_link #4429
Unanswered
emi0000
asked this question in
Looking for help
Replies: 2 comments 15 replies
-
You were almost there! This works: import pymupdf as fz # do not use the deprecated name fitz
import matplotlib.pyplot as plt
filename = "mlf.pdf"
plt.plot([-2, 2], [1, 1])
plt.ylim(-1, 2)
plt.text(1, 1.5, "mytext")
plt.text(1, 0.5, "mytext")
plt.text(0.5, 0.5, "text")
plt.savefig(filename)
doc1 = fz.open(filename)
doc2 = fz.open(filename)
doc1.insert_pdf(doc2)
findtext = "mytext"
page = doc1[0]
# you need to set quads=False (default) to get the rects of the text!
rec = page.search_for(findtext, quads=False)
page.add_highlight_annot(rec)
for rc in rec:
page.insert_link(
{"kind": fz.LINK_GOTO, "page": 1, "from": rc, "to": fz.Point(0.0, 0.0)}
)
doc1.ez_save("both_linked.pdf") # use best compression A bit unrelated, but here is an alternative that only requires opening 1 document: filename = "mlf.pdf"
plt.plot([-2, 2], [1, 1])
plt.ylim(-1, 2)
plt.text(1, 1.5, "mytext")
plt.text(1, 0.5, "mytext")
plt.text(0.5, 0.5, "text")
plt.savefig(filename)
doc = fz.open(filename)
findtext = "mytext"
doc.fullcopy_page(0) # full copy of page 0
page = doc[0]
# you need to set quads=False (default) to get the rects of the text!
rec = page.search_for(findtext, quads=False)
page.add_highlight_annot(rec)
for rc in rec:
page.insert_link(
{"kind": fz.LINK_GOTO, "page": 1, "from": rc, "to": fz.Point(0.0, 0.0)}
)
doc.ez_save("both_linked1.pdf") # use best compression |
Beta Was this translation helpful? Give feedback.
8 replies
-
I'm generating the pdfs from matplotlib. I tried to make a small example out of what I'm doing.
|
Beta Was this translation helpful? Give feedback.
7 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
I'm trying to insert a link on page 0 of a pdf to page 1 of the same pdf. I tried also non-matplotlib pdfs or file with different pages. I always get an error message. My code looks as follows
the rror message looks as follows
Actually I wanted to make a link from "mytext" on page 0 to "text" on page 1.
Any ideas? thanks
Beta Was this translation helpful? Give feedback.
All reactions