@@ -1127,14 +1127,14 @@ def update_page_form_field_values(
11271127 if PG .ANNOTS not in page :
11281128 logger_warning ("No fields to update on this page" , __name__ )
11291129 return
1130- for annotation in page [PG .ANNOTS ]: # type: ignore
1131- annotation_obj = cast (DictionaryObject , annotation .get_object ())
1132- if annotation_obj .get ("/Subtype" , "" ) != "/Widget" :
1130+ for annotation_ref in page [PG .ANNOTS ]: # type: ignore
1131+ annotation = cast (DictionaryObject , annotation_ref .get_object ())
1132+ if annotation .get ("/Subtype" , "" ) != "/Widget" :
11331133 continue
1134- if "/FT" in annotation_obj and "/T" in annotation_obj :
1135- parent_annotation = annotation_obj
1134+ if "/FT" in annotation and "/T" in annotation :
1135+ parent_annotation = annotation
11361136 else :
1137- parent_annotation = annotation_obj .get (
1137+ parent_annotation = annotation .get (
11381138 PG .PARENT , DictionaryObject ()
11391139 ).get_object ()
11401140
@@ -1150,32 +1150,32 @@ def update_page_form_field_values(
11501150 ):
11511151 del parent_annotation ["/I" ]
11521152 if flags :
1153- annotation_obj [NameObject (FA .Ff )] = NumberObject (flags )
1153+ annotation [NameObject (FA .Ff )] = NumberObject (flags )
11541154 if not (value is None and flatten ): # Only change values if given by user and not flattening.
11551155 if isinstance (value , list ):
11561156 lst = ArrayObject (TextStringObject (v ) for v in value )
11571157 parent_annotation [NameObject (FA .V )] = lst
11581158 elif isinstance (value , tuple ):
1159- annotation_obj [NameObject (FA .V )] = TextStringObject (
1159+ annotation [NameObject (FA .V )] = TextStringObject (
11601160 value [0 ],
11611161 )
11621162 else :
11631163 parent_annotation [NameObject (FA .V )] = TextStringObject (value )
11641164 if parent_annotation .get (FA .FT ) == "/Btn" :
11651165 # Checkbox button (no /FT found in Radio widgets)
11661166 v = NameObject (value )
1167- ap = cast (DictionaryObject , annotation_obj [NameObject (AA .AP )])
1167+ ap = cast (DictionaryObject , annotation [NameObject (AA .AP )])
11681168 normal_ap = cast (DictionaryObject , ap ["/N" ])
11691169 if v not in normal_ap :
11701170 v = NameObject ("/Off" )
11711171 appearance_stream_obj = normal_ap .get (v )
11721172 # other cases will be updated through the for loop
1173- annotation_obj [NameObject (AA .AS )] = v
1174- annotation_obj [NameObject (FA .V )] = v
1173+ annotation [NameObject (AA .AS )] = v
1174+ annotation [NameObject (FA .V )] = v
11751175 if flatten and appearance_stream_obj is not None :
11761176 # We basically copy the entire appearance stream, which should be an XObject that
11771177 # is already registered. No need to add font resources.
1178- rct = cast (RectangleObject , annotation_obj [AA .Rect ])
1178+ rct = cast (RectangleObject , annotation [AA .Rect ])
11791179 self ._add_apstream_object (page , appearance_stream_obj , field , rct [0 ], rct [1 ])
11801180 elif (
11811181 parent_annotation .get (FA .FT ) == "/Tx"
@@ -1184,12 +1184,12 @@ def update_page_form_field_values(
11841184 # textbox
11851185 if isinstance (value , tuple ):
11861186 self ._update_field_annotation (
1187- page , parent_annotation , annotation_obj , value [1 ], value [2 ], flatten = flatten
1187+ page , parent_annotation , annotation , value [1 ], value [2 ], flatten = flatten
11881188 )
11891189 else :
1190- self ._update_field_annotation (page , parent_annotation , annotation_obj , flatten = flatten )
1190+ self ._update_field_annotation (page , parent_annotation , annotation , flatten = flatten )
11911191 elif (
1192- annotation_obj .get (FA .FT ) == "/Sig"
1192+ annotation .get (FA .FT ) == "/Sig"
11931193 ): # deprecated # not implemented yet
11941194 logger_warning ("Signature forms not implemented yet" , __name__ )
11951195
@@ -1228,19 +1228,19 @@ def reattach_fields(
12281228 if "/Annots" not in page :
12291229 return lst
12301230 annotations = cast (ArrayObject , page ["/Annots" ])
1231- for idx , annotation in enumerate (annotations ):
1232- is_indirect = isinstance (annotation , IndirectObject )
1233- annotation_obj = cast (DictionaryObject , annotation .get_object ())
1234- if annotation_obj .get ("/Subtype" , "" ) == "/Widget" and "/FT" in annotation_obj :
1231+ for idx , annotation_ref in enumerate (annotations ):
1232+ is_indirect = isinstance (annotation_ref , IndirectObject )
1233+ annotation = cast (DictionaryObject , annotation_ref .get_object ())
1234+ if annotation .get ("/Subtype" , "" ) == "/Widget" and "/FT" in annotation :
12351235 if (
1236- "indirect_reference" in annotation_obj .__dict__
1237- and annotation_obj .indirect_reference in fields
1236+ "indirect_reference" in annotation .__dict__
1237+ and annotation .indirect_reference in fields
12381238 ):
12391239 continue
12401240 if not is_indirect :
1241- annotations [idx ] = self ._add_object (annotation_obj )
1242- fields .append (annotation_obj .indirect_reference )
1243- lst .append (annotation_obj )
1241+ annotations [idx ] = self ._add_object (annotation )
1242+ fields .append (annotation .indirect_reference )
1243+ lst .append (annotation )
12441244 return lst
12451245
12461246 def clone_reader_document_root (self , reader : PdfReader ) -> None :
@@ -1600,11 +1600,10 @@ def _write_pdf_structure(self, stream: StreamType) -> tuple[list[int], list[int]
16001600 if obj is not None :
16011601 object_positions .append (stream .tell ())
16021602 stream .write (f"{ idnum } 0 obj\n " .encode ())
1603+ object_to_write = obj
16031604 if self ._encryption and obj != self ._encrypt_entry :
1604- obj_to_write = self ._encryption .encrypt_object (obj , idnum , 0 )
1605- else :
1606- obj_to_write = obj
1607- obj_to_write .write_to_stream (stream )
1605+ object_to_write = self ._encryption .encrypt_object (obj , idnum , 0 )
1606+ object_to_write .write_to_stream (stream )
16081607 stream .write (b"\n endobj\n " )
16091608 else :
16101609 object_positions .append (- 1 )
@@ -1693,10 +1692,7 @@ def add_metadata(self, infos: dict[str, Any]) -> None:
16931692 if isinstance (infos , PdfObject ):
16941693 infos = cast (DictionaryObject , infos .get_object ())
16951694 for key , value in list (infos .items ()):
1696- if isinstance (value , PdfObject ):
1697- value_obj = value .get_object ()
1698- else :
1699- value_obj = value
1695+ value_obj = value .get_object () if isinstance (value , PdfObject ) else value
17001696 args [NameObject (key )] = create_string_object (str (value_obj ))
17011697 if self ._info is None :
17021698 self ._info = DictionaryObject ()
0 commit comments