Skip to content

Commit 487a9cb

Browse files
ROB: Allow merging when annotations miss D entry (#3281)
Closes #3211. Replaces #3213.
1 parent 24b3a8f commit 487a9cb

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

pypdf/_writer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3043,8 +3043,8 @@ def _insert_filtered_annotations(
30433043
anc[NameObject("/Dest")] = ArrayObject([p] + d[1:])
30443044
outlist.append(self._add_object(anc))
30453045
else:
3046-
d = cast("DictionaryObject", ano["/A"])["/D"]
3047-
if isinstance(d, NullObject):
3046+
d = cast("DictionaryObject", ano["/A"]).get("/D", NullObject())
3047+
if d is None or isinstance(d, NullObject):
30483048
continue
30493049
if isinstance(d, str):
30503050
# it is a named dest

tests/test_writer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2714,3 +2714,33 @@ def test_merge__process_named_dests__no_dests_in_source_file():
27142714
assert writer.named_destinations == {
27152715
"test.pdf": Destination(title="test.pdf", page=writer.pages[1].indirect_reference, fit=Fit("/Fit"))
27162716
}
2717+
2718+
2719+
def test_insert_filtered_annotations__link_without_destination():
2720+
"""Test for #3211"""
2721+
writer = PdfWriter(clone_from=RESOURCE_ROOT / "crazyones.pdf")
2722+
reader = PdfReader(RESOURCE_ROOT / "hello-world.pdf")
2723+
2724+
annotations = [
2725+
DictionaryObject({
2726+
"/A": DictionaryObject({"/S": NameObject("/GoTo"), "/D": None}),
2727+
"/BS": {"/S": "/S", "/Type": "/Border", "/W": 0},
2728+
"/Border": [0, 0, 0],
2729+
"/H": "/I",
2730+
"/Rect": [68.6001, 653.405, 526.2, 671.054],
2731+
"/StructParent": 9,
2732+
"/Subtype": NameObject("/Link"),
2733+
"/Type": NameObject("/Annot")
2734+
})
2735+
]
2736+
result = writer._insert_filtered_annotations(
2737+
annots=annotations, page=writer.pages[0], pages={}, reader=reader
2738+
)
2739+
assert result == []
2740+
2741+
writer = PdfWriter(clone_from=RESOURCE_ROOT / "crazyones.pdf")
2742+
del annotations[0]["/A"]["/D"]
2743+
result = writer._insert_filtered_annotations(
2744+
annots=annotations, page=writer.pages[0], pages={}, reader=reader
2745+
)
2746+
assert result == []

0 commit comments

Comments
 (0)