@@ -1036,28 +1036,13 @@ def update_page_form_field_values(
10361036 parent_annotation .get (FA .FT ) == "/Tx"
10371037 or parent_annotation .get (FA .FT ) == "/Ch"
10381038 ):
1039- # Textbox; we need to generate the appearance stream object
1039+ # Textbox; update appearance via helper
10401040 if isinstance (value , tuple ):
1041- appearance_stream_obj = TextStreamAppearance . from_text_annotation (
1042- acro_form , parent_annotation , annotation , value [1 ], value [2 ]
1041+ self . _update_field_annotation (
1042+ page , parent_annotation , annotation , value [1 ], value [2 ], flatten = flatten
10431043 )
10441044 else :
1045- appearance_stream_obj = TextStreamAppearance .from_text_annotation (
1046- acro_form , parent_annotation , annotation
1047- )
1048- # Add the appearance stream object
1049- if AA .AP not in annotation :
1050- annotation [NameObject (AA .AP )] = DictionaryObject (
1051- {NameObject ("/N" ): self ._add_object (appearance_stream_obj )}
1052- )
1053- elif "/N" not in (ap := cast (DictionaryObject , annotation [AA .AP ])):
1054- cast (DictionaryObject , annotation [NameObject (AA .AP )])[
1055- NameObject ("/N" )
1056- ] = self ._add_object (appearance_stream_obj )
1057- else : # [/AP][/N] exists
1058- n = annotation [AA .AP ]["/N" ].indirect_reference .idnum # type: ignore
1059- self ._objects [n - 1 ] = appearance_stream_obj
1060- appearance_stream_obj .indirect_reference = IndirectObject (n , 0 , self )
1045+ self ._update_field_annotation (page , parent_annotation , annotation , flatten = flatten )
10611046 elif (
10621047 annotation .get (FA .FT ) == "/Sig"
10631048 ): # deprecated # not implemented yet
@@ -1100,19 +1085,19 @@ def reattach_fields(
11001085 if "/Annots" not in page :
11011086 return lst
11021087 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 :
1088+ for idx , annotation_ref in enumerate (annotations ):
1089+ is_indirect = isinstance (annotation_ref , IndirectObject )
1090+ annotation = cast (DictionaryObject , annotation_ref .get_object ())
1091+ if annotation .get ("/Subtype" , "" ) == "/Widget" and "/FT" in annotation :
11071092 if (
1108- "indirect_reference" in annotation_obj .__dict__
1109- and annotation_obj .indirect_reference in fields
1093+ "indirect_reference" in annotation .__dict__
1094+ and annotation .indirect_reference in fields
11101095 ):
11111096 continue
11121097 if not is_indirect :
1113- annotations [idx ] = self ._add_object (annotation_obj )
1114- fields .append (annotation_obj .indirect_reference )
1115- lst .append (annotation_obj )
1098+ annotations [idx ] = self ._add_object (annotation )
1099+ fields .append (annotation .indirect_reference )
1100+ lst .append (annotation )
11161101 return lst
11171102
11181103 def clone_reader_document_root (self , reader : PdfReader ) -> None :
@@ -1472,11 +1457,10 @@ def _write_pdf_structure(self, stream: StreamType) -> tuple[list[int], list[int]
14721457 if obj is not None :
14731458 object_positions .append (stream .tell ())
14741459 stream .write (f"{ idnum } 0 obj\n " .encode ())
1460+ object_to_write = obj
14751461 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 )
1462+ object_to_write = self ._encryption .encrypt_object (obj , idnum , 0 )
1463+ object_to_write .write_to_stream (stream )
14801464 stream .write (b"\n endobj\n " )
14811465 else :
14821466 object_positions .append (- 1 )
@@ -1565,10 +1549,7 @@ def add_metadata(self, infos: dict[str, Any]) -> None:
15651549 if isinstance (infos , PdfObject ):
15661550 infos = cast (DictionaryObject , infos .get_object ())
15671551 for key , value in list (infos .items ()):
1568- if isinstance (value , PdfObject ):
1569- value_obj = value .get_object ()
1570- else :
1571- value_obj = value
1552+ value_obj = value .get_object () if isinstance (value , PdfObject ) else value
15721553 args [NameObject (key )] = create_string_object (str (value_obj ))
15731554 if self ._info is None :
15741555 self ._info = DictionaryObject ()
0 commit comments