Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Scale PDF annotations #1479

Merged
merged 3 commits into from Dec 10, 2022
Merged

Conversation

joshhendo
Copy link
Contributor

@joshhendo joshhendo commented Dec 9, 2022

Fixes #1386

PDF annotations - for example hyperlinks and comments - aren't properly scaled when using the scale function.

I tried to follow a similar pattern to the code just below my change which scales the Viewport.

As a test, the input PDF is as follows:
image

In this screenshot, I am clicking on the link and Adobe Acrobat shows it as a black box. This is in the position I expect.

When scaled to 0.9 without this fix, the following occurs:
image

The clickable area for the link has changed, as well as the comment.

The PDF used to test was:
test_input.pdf

The PDF scaled to 0.9 before the fix:
test_output_before.pdf

The PDF scaled to 0.9 after the fix:
test_output_after.pdf

@joshhendo
Copy link
Contributor Author

I'm not sure why the CI is failing indicating that the annotations aren't iterable - it seems to be when I run it and I based the code on the docs here: https://pypdf2.readthedocs.io/en/latest/user/reading-pdf-annotations.html?highlight=annotations

Perhaps it's not guaranteed to be iterable and there needs to be some form of type guard before attempting to iterate it.

PyPDF2/_page.py Outdated Show resolved Hide resolved
@codecov
Copy link

codecov bot commented Dec 10, 2022

Codecov Report

Base: 93.97% // Head: 93.93% // Decreases project coverage by -0.04% ⚠️

Coverage data is based on head (2a060d9) compared to base (b1938aa).
Patch coverage: 76.92% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1479      +/-   ##
==========================================
- Coverage   93.97%   93.93%   -0.05%     
==========================================
  Files          30       30              
  Lines        5446     5459      +13     
  Branches     1039     1044       +5     
==========================================
+ Hits         5118     5128      +10     
  Misses        198      198              
- Partials      130      133       +3     
Impacted Files Coverage Δ
PyPDF2/_page.py 91.92% <76.92%> (-0.30%) ⬇️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@MartinThoma MartinThoma merged commit 5fd96d0 into py-pdf:main Dec 10, 2022
@MartinThoma
Copy link
Member

@joshhendo Thank you for taking care of it 🙏 I'll release it to PyPI today :-)

If you want, I can add you to the list of contributors: https://pypdf2.readthedocs.io/en/latest/meta/CONTRIBUTORS.html :-)

MartinThoma added a commit that referenced this pull request Dec 10, 2022
New Features (ENH):
-  Add support to extract gray scale images (#1460)
-  Add 'threads' property to PdfWriter (#1458)
-  Add 'open_destination' property to PdfWriter (#1431)
-  Make PdfReader.get_object accept integer arguments (#1459)

Bug Fixes (BUG):
-  Scale PDF annotations (#1479)

Robustness (ROB):
-  Padding issue with AES encryption (#1469)
-  Accept empty object as null objects (#1477)

Documentation (DOC):
-  Add module documentation the PaperSize class (#1447)

Maintenance (MAINT):
-  Use 'page_number' instead of 'pagenum' (#1365)
-  Add List of pages to PageRangeSpec (#1456)

Testing (TST):
-  Cleanup temporary files (#1454)
-  Mark test_tounicode_is_identity as external (#1449)
-  Use Ubuntu 20.04 for running CI test suite (#1452)

[Full Changelog](2.11.2...2.12.0)
@joshhendo
Copy link
Contributor Author

Thanks @MartinThoma ! And for fixing the mypy issue - I was on the right path thinking that narrowing/a type guard was needed. I have never used mypy before so I had been trying to get it working locally for me to work on.

@joshhendo
Copy link
Contributor Author

If you want, I can add you to the list of contributors: https://pypdf2.readthedocs.io/en/latest/meta/CONTRIBUTORS.html :-)

That would be great!

@pubpub-zz
Copy link
Collaborator

@joshhendo / @MartinThoma
This PR only fixes scaling but not the other transformations. Actually, the annotations reposition should be applied within the add_transformation function.
example with a rotation of -10deg
image

@joshhendo, are you OK to upgrade your code change ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Page transformations are not applied to annotations
3 participants