pypdf icon indicating copy to clipboard operation
pypdf copied to clipboard

ENH: Modify page attributes that depend on page orientation after rotating page

Open thommiano opened this issue 2 years ago • 0 comments

Rotating a page, e.g., using page.RotateClockwise() only updates the /Rotate attribute:

def rotateClockwise(self, angle):
        """
        Rotates a page clockwise by increments of 90 degrees.

        :param int angle: Angle to rotate the page.  Must be an increment
            of 90 deg.
        """
        assert angle % 90 == 0
        self._rotate(angle)
        return self

def rotateCounterClockwise(self, angle):
        """
        Rotates a page counter-clockwise by increments of 90 degrees.

        :param int angle: Angle to rotate the page.  Must be an increment
            of 90 deg.
        """
        assert angle % 90 == 0
        self._rotate(-angle)
        return self

def _rotate(self, angle):
        currentAngle = self.get("/Rotate", 0)
        self[NameObject("/Rotate")] = NumberObject(currentAngle + angle)

The expected behavior is that other attributes that depend on a page's origin should also be updated based on rotation. For example:

  • /mediaBox - The x, y dimensions of the page. If the page is rotated 90 degrees, the values in the mediaBox should swap places
  • /Annots.../Rect - The coordinates of a rectangular bounding box for an annotation. These coordinates are [x0, y0, x1, y1], with the origin being at the bottom left of the page. If the page is rotated, the origin with respect to the annotations according from the users perspective has changed and so should the coordinates.

Notes

I created this issue to capture a more general issue that is happening in more specific cases that haven't been resolved:

  • https://github.com/mstamy2/PyPDF2/issues/525#issue-529556145
  • https://github.com/mstamy2/PyPDF2/issues/558#issue-629515115

thommiano avatar Aug 05 '21 20:08 thommiano