Skip to content

Commit 6da4f06

Browse files
redef error and write/form tests error fixed
1 parent 12353fd commit 6da4f06

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
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: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -978,14 +978,14 @@ def update_page_form_field_values(
978978
if PG.ANNOTS not in page:
979979
logger_warning("No fields to update on this page", __name__)
980980
return
981-
for annotation in page[PG.ANNOTS]: # type: ignore
982-
annotation_obj = cast(DictionaryObject, annotation.get_object())
983-
if annotation_obj.get("/Subtype", "") != "/Widget":
981+
for annotation_ref in page[PG.ANNOTS]: # type: ignore
982+
annotation = cast(DictionaryObject, annotation_ref.get_object())
983+
if annotation.get("/Subtype", "") != "/Widget":
984984
continue
985-
if "/FT" in annotation_obj and "/T" in annotation_obj:
986-
parent_annotation = annotation_obj
985+
if "/FT" in annotation and "/T" in annotation:
986+
parent_annotation = annotation
987987
else:
988-
parent_annotation = annotation_obj.get(
988+
parent_annotation = annotation.get(
989989
PG.PARENT, DictionaryObject()
990990
).get_object()
991991

@@ -1003,7 +1003,6 @@ def update_page_form_field_values(
10031003
del parent_annotation["/I"]
10041004
if flags:
10051005
annotation[NameObject(FA.Ff)] = NumberObject(flags)
1006-
# Set the field value
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)
@@ -1025,9 +1024,14 @@ def update_page_form_field_values(
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
1027+
# other cases will be updated through the for loop
10291028
annotation[NameObject(AA.AS)] = v
10301029
annotation[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[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"
@@ -1046,7 +1050,7 @@ def update_page_form_field_values(
10461050
annotation[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])):
1053+
elif "/N" not in (ap := cast(DictionaryObject, annotation[AA.AP])):
10501054
cast(DictionaryObject, annotation[NameObject(AA.AP)])[
10511055
NameObject("/N")
10521056
] = self._add_object(appearance_stream_obj)

0 commit comments

Comments
 (0)