@@ -978,19 +978,19 @@ 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
992992 for field , value in fields .items ():
993- rectangle = cast (RectangleObject , annotation_obj [AA .Rect ])
993+ rectangle = cast (RectangleObject , annotation [AA .Rect ])
994994 if not (
995995 self ._get_qualified_field_name (parent_annotation ) == field
996996 or parent_annotation .get ("/T" , None ) == field
@@ -1002,13 +1002,13 @@ def update_page_form_field_values(
10021002 ):
10031003 del parent_annotation ["/I" ]
10041004 if flags :
1005- annotation_obj [NameObject (FA .Ff )] = NumberObject (flags )
1005+ annotation [NameObject (FA .Ff )] = NumberObject (flags )
10061006 if not (value is None and flatten ): # Only change values if given by user and not flattening.
10071007 if isinstance (value , list ):
10081008 lst = ArrayObject (TextStringObject (v ) for v in value )
10091009 parent_annotation [NameObject (FA .V )] = lst
10101010 elif isinstance (value , tuple ):
1011- annotation_obj [NameObject (FA .V )] = TextStringObject (
1011+ annotation [NameObject (FA .V )] = TextStringObject (
10121012 value [0 ],
10131013 )
10141014 else :
@@ -1019,18 +1019,18 @@ def update_page_form_field_values(
10191019 # We can find the associated appearance stream object
10201020 # within the annotation.
10211021 v = NameObject (value )
1022- ap = cast (DictionaryObject , annotation_obj [NameObject (AA .AP )])
1022+ ap = cast (DictionaryObject , annotation [NameObject (AA .AP )])
10231023 normal_ap = cast (DictionaryObject , ap ["/N" ])
10241024 if v not in normal_ap :
10251025 v = NameObject ("/Off" )
10261026 appearance_stream_obj = normal_ap .get (v )
10271027 # other cases will be updated through the for loop
1028- annotation_obj [NameObject (AA .AS )] = v
1029- annotation_obj [NameObject (FA .V )] = v
1028+ annotation [NameObject (AA .AS )] = v
1029+ annotation [NameObject (FA .V )] = v
10301030 if flatten and appearance_stream_obj is not None :
10311031 # We basically copy the entire appearance stream, which should be an XObject that
10321032 # is already registered. No need to add font resources.
1033- rct = cast (RectangleObject , annotation_obj [AA .Rect ])
1033+ rct = cast (RectangleObject , annotation [AA .Rect ])
10341034 self ._add_apstream_object (page , appearance_stream_obj , field , rct [0 ], rct [1 ])
10351035 elif (
10361036 parent_annotation .get (FA .FT ) == "/Tx"
@@ -1039,27 +1039,27 @@ def update_page_form_field_values(
10391039 # Textbox; we need to generate the appearance stream object
10401040 if isinstance (value , tuple ):
10411041 appearance_stream_obj = TextStreamAppearance .from_text_annotation (
1042- acro_form , parent_annotation , annotation_obj , value [1 ], value [2 ]
1042+ acro_form , parent_annotation , annotation , value [1 ], value [2 ]
10431043 )
10441044 else :
10451045 appearance_stream_obj = TextStreamAppearance .from_text_annotation (
1046- acro_form , parent_annotation , annotation_obj
1046+ acro_form , parent_annotation , annotation
10471047 )
10481048 # Add the appearance stream object
1049- if AA .AP not in annotation_obj :
1050- annotation_obj [NameObject (AA .AP )] = DictionaryObject (
1049+ if AA .AP not in annotation :
1050+ annotation [NameObject (AA .AP )] = DictionaryObject (
10511051 {NameObject ("/N" ): self ._add_object (appearance_stream_obj )}
10521052 )
1053- elif "/N" not in (ap := cast (DictionaryObject , annotation_obj [AA .AP ])):
1054- cast (DictionaryObject , annotation_obj [NameObject (AA .AP )])[
1053+ elif "/N" not in (ap := cast (DictionaryObject , annotation [AA .AP ])):
1054+ cast (DictionaryObject , annotation [NameObject (AA .AP )])[
10551055 NameObject ("/N" )
10561056 ] = self ._add_object (appearance_stream_obj )
10571057 else : # [/AP][/N] exists
1058- n = annotation_obj [AA .AP ]["/N" ].indirect_reference .idnum # type: ignore
1058+ n = annotation [AA .AP ]["/N" ].indirect_reference .idnum # type: ignore
10591059 self ._objects [n - 1 ] = appearance_stream_obj
10601060 appearance_stream_obj .indirect_reference = IndirectObject (n , 0 , self )
10611061 elif (
1062- annotation_obj .get (FA .FT ) == "/Sig"
1062+ annotation .get (FA .FT ) == "/Sig"
10631063 ): # deprecated # not implemented yet
10641064 logger_warning ("Signature forms not implemented yet" , __name__ )
10651065 if flatten and appearance_stream_obj is not None :
@@ -1100,19 +1100,19 @@ def reattach_fields(
11001100 if "/Annots" not in page :
11011101 return lst
11021102 annotations = cast (ArrayObject , page ["/Annots" ])
1103- for idx , annotation in enumerate (annotations ):
1104- is_indirect = isinstance (annotation , IndirectObject )
1105- annotation_obj = cast (DictionaryObject , annotation .get_object ())
1106- if annotation_obj .get ("/Subtype" , "" ) == "/Widget" and "/FT" in annotation_obj :
1103+ for idx , annotation_ref in enumerate (annotations ):
1104+ is_indirect = isinstance (annotation_ref , IndirectObject )
1105+ annotation = cast (DictionaryObject , annotation_ref .get_object ())
1106+ if annotation .get ("/Subtype" , "" ) == "/Widget" and "/FT" in annotation :
11071107 if (
1108- "indirect_reference" in annotation_obj .__dict__
1109- and annotation_obj .indirect_reference in fields
1108+ "indirect_reference" in annotation .__dict__
1109+ and annotation .indirect_reference in fields
11101110 ):
11111111 continue
11121112 if not is_indirect :
1113- annotations [idx ] = self ._add_object (annotation_obj )
1114- fields .append (annotation_obj .indirect_reference )
1115- lst .append (annotation_obj )
1113+ annotations [idx ] = self ._add_object (annotation )
1114+ fields .append (annotation .indirect_reference )
1115+ lst .append (annotation )
11161116 return lst
11171117
11181118 def clone_reader_document_root (self , reader : PdfReader ) -> None :
@@ -1472,11 +1472,10 @@ def _write_pdf_structure(self, stream: StreamType) -> tuple[list[int], list[int]
14721472 if obj is not None :
14731473 object_positions .append (stream .tell ())
14741474 stream .write (f"{ idnum } 0 obj\n " .encode ())
1475+ object_to_write = obj
14751476 if self ._encryption and obj != self ._encrypt_entry :
1476- obj_to_write = self ._encryption .encrypt_object (obj , idnum , 0 )
1477- else :
1478- obj_to_write = obj
1479- obj_to_write .write_to_stream (stream )
1477+ object_to_write = self ._encryption .encrypt_object (obj , idnum , 0 )
1478+ object_to_write .write_to_stream (stream )
14801479 stream .write (b"\n endobj\n " )
14811480 else :
14821481 object_positions .append (- 1 )
@@ -1565,10 +1564,7 @@ def add_metadata(self, infos: dict[str, Any]) -> None:
15651564 if isinstance (infos , PdfObject ):
15661565 infos = cast (DictionaryObject , infos .get_object ())
15671566 for key , value in list (infos .items ()):
1568- if isinstance (value , PdfObject ):
1569- value_obj = value .get_object ()
1570- else :
1571- value_obj = value
1567+ value_obj = value .get_object () if isinstance (value , PdfObject ) else value
15721568 args [NameObject (key )] = create_string_object (str (value_obj ))
15731569 if self ._info is None :
15741570 self ._info = DictionaryObject ()
0 commit comments