From 912b50cfd574919c1086f0337c060bd1267da134 Mon Sep 17 00:00:00 2001 From: Stefan <96178532+stefan6419846@users.noreply.github.com> Date: Sun, 9 Feb 2025 10:10:02 +0100 Subject: [PATCH] BUG: Handle annotations being None on merging (#3111) Closes #3110. --- pypdf/_writer.py | 4 +++- tests/test_writer.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/pypdf/_writer.py b/pypdf/_writer.py index a67c270d4..9fb460b56 100644 --- a/pypdf/_writer.py +++ b/pypdf/_writer.py @@ -2932,7 +2932,7 @@ def _get_cloned_page( def _insert_filtered_annotations( self, - annots: Union[IndirectObject, List[DictionaryObject]], + annots: Union[IndirectObject, List[DictionaryObject], None], page: PageObject, pages: Dict[int, PageObject], reader: PdfReader, @@ -2940,6 +2940,8 @@ def _insert_filtered_annotations( outlist = ArrayObject() if isinstance(annots, IndirectObject): annots = cast("List[Any]", annots.get_object()) + if annots is None: + return outlist for an in annots: ano = cast("DictionaryObject", an.get_object()) if ( diff --git a/tests/test_writer.py b/tests/test_writer.py index fa8ffadef..939e2f847 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -2607,3 +2607,14 @@ def test_inline_image_q_operator_handling(tmp_path): ) assert png_path.is_file() assert image_similarity(png_path, expected_png_path) >= 0.99999 + + +def test_insert_filtered_annotations__annotations_are_none(): + writer = PdfWriter() + writer.add_blank_page(72, 72) + stream = BytesIO() + writer.write(stream) + reader = PdfReader(stream) + assert writer._insert_filtered_annotations( + annots=None, page=PageObject(), pages={}, reader=reader + ) == []