|
30 | 30 |
|
31 | 31 | from typing import TYPE_CHECKING, Optional, Union, cast |
32 | 32 |
|
33 | | -from . import ArrayObject, DictionaryObject, IndirectObject, PdfObject, TextStringObject |
| 33 | +from .._utils import logger_warning |
| 34 | +from . import ArrayObject, DictionaryObject, IndirectObject, PdfObject, TextStringObject, is_null_or_none |
34 | 35 |
|
35 | 36 | if TYPE_CHECKING: |
36 | 37 | from .._page import PageObject |
@@ -79,8 +80,23 @@ def extract_links(new_page: "PageObject", old_page: "PageObject") -> list[tuple[ |
79 | 80 | """Extracts links from two pages on the assumption that the two pages are |
80 | 81 | the same. Produces one list of (new link, old link) tuples. |
81 | 82 | """ |
82 | | - new_links = [_build_link(link, new_page) for link in new_page.get("/Annots", [])] |
83 | | - old_links = [_build_link(link, old_page) for link in old_page.get("/Annots", [])] |
| 83 | + new_annotations = new_page.get("/Annots", ArrayObject()).get_object() |
| 84 | + old_annotations = old_page.get("/Annots", ArrayObject()).get_object() |
| 85 | + if is_null_or_none(new_annotations): |
| 86 | + new_annotations = ArrayObject() |
| 87 | + if is_null_or_none(old_annotations): |
| 88 | + old_annotations = ArrayObject() |
| 89 | + if not isinstance(new_annotations, ArrayObject) or not isinstance(old_annotations, ArrayObject): |
| 90 | + logger_warning( |
| 91 | + f"Expected annotation arrays: {old_annotations} {new_annotations}. Ignoring annotations.", |
| 92 | + __name__ |
| 93 | + ) |
| 94 | + return [] |
| 95 | + if len(new_annotations) != len(old_annotations): |
| 96 | + logger_warning(f"Annotation sizes differ: {old_annotations} vs. {new_annotations}", __name__) |
| 97 | + |
| 98 | + new_links = [_build_link(link, new_page) for link in new_annotations] |
| 99 | + old_links = [_build_link(link, old_page) for link in old_annotations] |
84 | 100 |
|
85 | 101 | return [ |
86 | 102 | (new_link, old_link) for (new_link, old_link) |
@@ -110,7 +126,7 @@ def _build_link(indirect_object: IndirectObject, page: "PageObject") -> Optional |
110 | 126 | return None # Nothing to do here |
111 | 127 |
|
112 | 128 |
|
113 | | -def _create_link(reference: PdfObject, source_pdf: "PdfReader")-> Optional[ReferenceLink]: |
| 129 | +def _create_link(reference: PdfObject, source_pdf: "PdfReader") -> Optional[ReferenceLink]: |
114 | 130 | if isinstance(reference, TextStringObject): |
115 | 131 | return NamedReferenceLink(reference, source_pdf) |
116 | 132 | if isinstance(reference, ArrayObject): |
|
0 commit comments