Skip to content

Commit e625cb6

Browse files
committed
MAINT: _writer: refactor update_page_form_fields
Add a couple of comments to the update_page_form_fields method, and change the flatten command later on. Underlying logic: First set the field value, then get its appearance stream, and, if it has one, flatten it if appropriate.
1 parent 9ea3f06 commit e625cb6

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

pypdf/_writer.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ def update_page_form_field_values(
11411141
del parent_annotation["/I"]
11421142
if flags:
11431143
annotation[NameObject(FA.Ff)] = NumberObject(flags)
1144+
# Set the field value
11441145
if not (value is None and flatten): # Only change values if given by user and not flattening.
11451146
if isinstance(value, list):
11461147
lst = ArrayObject(TextStringObject(v) for v in value)
@@ -1151,6 +1152,7 @@ def update_page_form_field_values(
11511152
)
11521153
else:
11531154
parent_annotation[NameObject(FA.V)] = TextStringObject(value)
1155+
# Get or create the field's appearance stream
11541156
if parent_annotation.get(FA.FT) == "/Btn":
11551157
# Checkbox button (no /FT found in Radio widgets)
11561158
v = NameObject(value)
@@ -1162,28 +1164,25 @@ def update_page_form_field_values(
11621164
# other cases will be updated through the for loop
11631165
annotation[NameObject(AA.AS)] = v
11641166
annotation[NameObject(FA.V)] = v
1165-
if flatten and appearance_stream_obj is not None:
1166-
# We basically copy the entire appearance stream, which should be an XObject that
1167-
# is already registered. No need to add font resources.
1168-
self._add_apstream_object(page, appearance_stream_obj, field, rct[0], rct[1])
11691167
elif (
11701168
parent_annotation.get(FA.FT) == "/Tx"
11711169
or parent_annotation.get(FA.FT) == "/Ch"
11721170
):
11731171
# textbox
11741172
if isinstance(value, tuple):
1175-
dct = self._update_field_annotation(
1173+
appearance_stream_obj = self._update_field_annotation(
11761174
page, parent_annotation, annotation, value[1], value[2]
11771175
)
11781176
else:
1179-
dct = self._update_field_annotation(page, parent_annotation, annotation)
1180-
if flatten:
1181-
field_name = self._get_qualified_field_name(annotation)
1182-
self._add_apstream_object(page, dct, field_name, rct[0], rct[1])
1177+
appearance_stream_obj = self._update_field_annotation(
1178+
page, parent_annotation, annotation
1179+
)
11831180
elif (
11841181
annotation.get(FA.FT) == "/Sig"
11851182
): # deprecated # not implemented yet
11861183
logger_warning("Signature forms not implemented yet", __name__)
1184+
if flatten and appearance_stream_obj is not None:
1185+
self._add_apstream_object(page, appearance_stream_obj, field, rct[0], rct[1])
11871186

11881187
def reattach_fields(
11891188
self, page: Optional[PageObject] = None

0 commit comments

Comments
 (0)