pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

Pagedest (addlink) not working as intended with positional parameters .

Open chukuwa-isobe opened this issue 1 year ago • 3 comments

If I create a link with positional, it jumps to an unintended page.

Environment

$ python -m platform
Windows-10-10.0

$ python -c "import PyPDF2;print(PyPDF2.__version__)"
2.10.7

Code + PDF

I want to use the "fit" option, so I write it positional.

from PyPDF2 import PdfWriter, PdfReader
from PyPDF2.generic import RectangleObject

in_file = r"D:\xxxxxxxxxxx\in.pdf"
out_file = r"D:\xxxxxxxxxxx\out.pdf"
writer = PdfWriter()
reader = PdfReader(in_file)

for current_page in reader.pages:
    writer.add_page(current_page)

writer.addLink(0, 1, [97, 758, 162, 768], [1, 1, 1], "/XYZ", 0, 0, 0)  # <-- here

with open(out_file, "wb") as o:
    writer.write(o)

Then when I click the link it jumps from page 1 to page "3". When I open the link properties with Acrobat, the link destination is set to page "2", and after closing the properties with OK without updating anything, clicking the link again correctly jumps to page "2". It's hard to open the properties of all created links and click OK, so please give me an idea how to solve it.

Traceback

This is the complete Traceback I see:

TODO

chukuwa-isobe avatar Nov 23 '22 14:11 chukuwa-isobe

@chukuwa-isobe, can you provde an example of pdf file (in and out) with the issue please

pubpub-zz avatar Nov 23 '22 18:11 pubpub-zz

Dear pubpub-zz,

Upload in.pdf and out.pdf. Two links are created on the first page in out.pdf. If I click on the first link, it jump to page 3 (issue...), but if I click on the second link, it jump to page 2. The pages specified should be the same.

from PyPDF2 import PdfFileWriter, PdfFileReader
from PyPDF2.generic import RectangleObject

inFile = r"xxxxxxxxxxxxxxxx\in.pdf"
outFile = r"xxxxxxxxxxxxxxxx\out.pdf"
pdf_writer = PdfFileWriter()
pdf_reader = PdfFileReader(open(inFile, "rb"))

num_of_pages = pdf_reader.getNumPages()

for page in range(num_of_pages):
    current_page = pdf_reader.getPage(page)
    pdf_writer.addPage(current_page)

pdf_writer.addLink(0, 1, [57, 721, 186, 731], [1, 1, 1], "/XYZ", 0, 0, 0)  # 1st BOX
pdf_writer.addLink(
    pagenum=0,
    pagedest=1,
    rect=RectangleObject([98, 708, 130, 718]),
    border=[1, 1, 1],
    fit="/XYZ",
)  # 2nd BOX

with open(outFile, "wb") as o:
    pdf_writer.write(o)

Is it not a good way to write?

in.pdf out.pdf

chukuwa-isobe avatar Nov 25 '22 15:11 chukuwa-isobe

Is there any progress on this?

When I try to run the "Add Link" example(s) from the documentation they don't work either...

corsair20141 avatar Mar 07 '23 18:03 corsair20141