From 5fd96d01c1febe6d38ba9b90ba774e95ceaca784 Mon Sep 17 00:00:00 2001 From: Josh Henderson Date: Sat, 10 Dec 2022 20:19:37 +1100 Subject: [PATCH] BUG: Scale PDF annotations (#1479) PDF annotations - for example hyperlinks and comments - were not properly scaled when using the scale function. Fixes #1386 --- PyPDF2/_page.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/PyPDF2/_page.py b/PyPDF2/_page.py index fcd6cb328..52863407c 100644 --- a/PyPDF2/_page.py +++ b/PyPDF2/_page.py @@ -58,6 +58,7 @@ from .constants import ImageAttributes as IA from .constants import PageAttributes as PG from .constants import Ressources as RES +from .constants import AnnotationDictionaryAttributes as ADA from .errors import PageSizeNotDefinedError from .filters import _xobj_to_image from .generic import ( @@ -1057,6 +1058,20 @@ def scale(self, sx: float, sy: float) -> None: self.bleedbox = self.bleedbox.scale(sx, sy) self.trimbox = self.trimbox.scale(sx, sy) self.mediabox = self.mediabox.scale(sx, sy) + + if PG.ANNOTS in self: + annotations = self[PG.ANNOTS] + if isinstance(annotations, ArrayObject): + for annotation in annotations: + annotation_obj = annotation.get_object() + if ADA.Rect in annotation_obj: + rectangle = annotation_obj[ADA.Rect] + if isinstance(rectangle, ArrayObject): + rectangle[0] = FloatObject(float(rectangle[0]) * sx) + rectangle[1] = FloatObject(float(rectangle[1]) * sy) + rectangle[2] = FloatObject(float(rectangle[2]) * sx) + rectangle[3] = FloatObject(float(rectangle[3]) * sy) + if PG.VP in self: viewport = self[PG.VP] if isinstance(viewport, ArrayObject):