Skip to content

Commit 9fca116

Browse files
redef error and write/form tests error fixed
1 parent 0292f57 commit 9fca116

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

pypdf/_page.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,12 +739,13 @@ def _get_inline_images(self) -> dict[str, ImageFile]:
739739
for k, v in ii["settings"].items():
740740
if k in {"/Length", "/L"}: # no length is expected
741741
continue
742+
value_for_init: Any
742743
if isinstance(v, list):
743-
value_for_init: Any = ArrayObject(
744+
value_for_init = ArrayObject(
744745
[self._translate_value_inline_image(k, x) for x in v]
745746
)
746747
else:
747-
value_for_init: Any = self._translate_value_inline_image(k, v)
748+
value_for_init = self._translate_value_inline_image(k, v)
748749
mapped_k = NameObject(_INLINE_IMAGE_KEY_MAPPING[k])
749750
if mapped_k not in init:
750751
init[mapped_k] = value_for_init

pypdf/_writer.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -990,7 +990,7 @@ def update_page_form_field_values(
990990
).get_object()
991991

992992
for field, value in fields.items():
993-
rectangle = cast(RectangleObject, annotation[AA.Rect])
993+
rectangle = cast(RectangleObject, annotation_obj[AA.Rect])
994994
if not (
995995
self._get_qualified_field_name(parent_annotation) == field
996996
or parent_annotation.get("/T", None) == field
@@ -1002,14 +1002,13 @@ def update_page_form_field_values(
10021002
):
10031003
del parent_annotation["/I"]
10041004
if flags:
1005-
annotation[NameObject(FA.Ff)] = NumberObject(flags)
1006-
# Set the field value
1005+
annotation_obj[NameObject(FA.Ff)] = NumberObject(flags)
10071006
if not (value is None and flatten): # Only change values if given by user and not flattening.
10081007
if isinstance(value, list):
10091008
lst = ArrayObject(TextStringObject(v) for v in value)
10101009
parent_annotation[NameObject(FA.V)] = lst
10111010
elif isinstance(value, tuple):
1012-
annotation[NameObject(FA.V)] = TextStringObject(
1011+
annotation_obj[NameObject(FA.V)] = TextStringObject(
10131012
value[0],
10141013
)
10151014
else:
@@ -1020,42 +1019,47 @@ def update_page_form_field_values(
10201019
# We can find the associated appearance stream object
10211020
# within the annotation.
10221021
v = NameObject(value)
1023-
ap = cast(DictionaryObject, annotation[NameObject(AA.AP)])
1022+
ap = cast(DictionaryObject, annotation_obj[NameObject(AA.AP)])
10241023
normal_ap = cast(DictionaryObject, ap["/N"])
10251024
if v not in normal_ap:
10261025
v = NameObject("/Off")
10271026
appearance_stream_obj = normal_ap.get(v)
1028-
# Other cases will be updated through the for loop
1029-
annotation[NameObject(AA.AS)] = v
1030-
annotation[NameObject(FA.V)] = v
1027+
# other cases will be updated through the for loop
1028+
annotation_obj[NameObject(AA.AS)] = v
1029+
annotation_obj[NameObject(FA.V)] = v
1030+
if flatten and appearance_stream_obj is not None:
1031+
# We basically copy the entire appearance stream, which should be an XObject that
1032+
# is already registered. No need to add font resources.
1033+
rct = cast(RectangleObject, annotation_obj[AA.Rect])
1034+
self._add_apstream_object(page, appearance_stream_obj, field, rct[0], rct[1])
10311035
elif (
10321036
parent_annotation.get(FA.FT) == "/Tx"
10331037
or parent_annotation.get(FA.FT) == "/Ch"
10341038
):
10351039
# Textbox; we need to generate the appearance stream object
10361040
if isinstance(value, tuple):
10371041
appearance_stream_obj = TextStreamAppearance.from_text_annotation(
1038-
acro_form, parent_annotation, annotation, value[1], value[2]
1042+
acro_form, parent_annotation, annotation_obj, value[1], value[2]
10391043
)
10401044
else:
10411045
appearance_stream_obj = TextStreamAppearance.from_text_annotation(
1042-
acro_form, parent_annotation, annotation
1046+
acro_form, parent_annotation, annotation_obj
10431047
)
10441048
# Add the appearance stream object
1045-
if AA.AP not in annotation:
1046-
annotation[NameObject(AA.AP)] = DictionaryObject(
1049+
if AA.AP not in annotation_obj:
1050+
annotation_obj[NameObject(AA.AP)] = DictionaryObject(
10471051
{NameObject("/N"): self._add_object(appearance_stream_obj)}
10481052
)
1049-
elif "/N" not in (ap:= cast(DictionaryObject, annotation[AA.AP])):
1050-
cast(DictionaryObject, annotation[NameObject(AA.AP)])[
1053+
elif "/N" not in (ap := cast(DictionaryObject, annotation_obj[AA.AP])):
1054+
cast(DictionaryObject, annotation_obj[NameObject(AA.AP)])[
10511055
NameObject("/N")
10521056
] = self._add_object(appearance_stream_obj)
10531057
else: # [/AP][/N] exists
1054-
n = annotation[AA.AP]["/N"].indirect_reference.idnum # type: ignore
1058+
n = annotation_obj[AA.AP]["/N"].indirect_reference.idnum # type: ignore
10551059
self._objects[n - 1] = appearance_stream_obj
10561060
appearance_stream_obj.indirect_reference = IndirectObject(n, 0, self)
10571061
elif (
1058-
annotation.get(FA.FT) == "/Sig"
1062+
annotation_obj.get(FA.FT) == "/Sig"
10591063
): # deprecated # not implemented yet
10601064
logger_warning("Signature forms not implemented yet", __name__)
10611065
if flatten and appearance_stream_obj is not None:

0 commit comments

Comments
 (0)