diff --git a/PyPDF2/_page.py b/PyPDF2/_page.py index 73ca6626e..726198a64 100644 --- a/PyPDF2/_page.py +++ b/PyPDF2/_page.py @@ -524,7 +524,13 @@ def _merge_page( page2content = page2.get_contents() if page2content is not None: page2content = ContentStream(page2content, self.pdf) - rect = page2.trimbox + if ( + page2.cropbox.width < page2.trimbox.width + or page2.cropbox.height < page2.trimbox.height + ): + rect = page2.cropbox + else: + rect = page2.trimbox page2content.operations.insert( 0, ( diff --git a/tests/test_page.py b/tests/test_page.py index 3e7caa33b..46c98888e 100644 --- a/tests/test_page.py +++ b/tests/test_page.py @@ -1,6 +1,7 @@ import json import os from copy import deepcopy +import io from io import BytesIO from pathlib import Path @@ -205,6 +206,21 @@ def test_page_rotation_non90(): assert exc.value.args[0] == "Rotation angle must be a multiple of 90" +def test_page_merge_cropped(): + p = PdfReader(os.path.join(RESOURCE_ROOT, "issue-604.pdf")) + a = deepcopy(p.pages[1]) # crossed to ease test reading + b = deepcopy(p.pages[2]) + a.cropbox = RectangleObject([100, 100, 300, 200]) + w = PdfWriter() + w.add_page(a) + w.add_page(b) + c = deepcopy(b) + c.merge_page(a) + w.add_page(c) + o = io.BytesIO() + w.write(o) + + def test_page_scale(): op = Transformation() with pytest.raises(ValueError) as exc: