@@ -932,6 +932,52 @@ def update_page_form_field_values(
932
932
value if value in k [AA .AP ]["/N" ] else "/Off"
933
933
)
934
934
935
+ def reattach_fields (
936
+ self , page : Optional [PageObject ] = None
937
+ ) -> List [DictionaryObject ]:
938
+ """
939
+ Parse annotations within the page looking for orphan fields and
940
+ reattach then into the Fields Structure
941
+
942
+ Args:
943
+ page: page to analyze.
944
+ If none is provided, all pages will be analyzed
945
+ Returns:
946
+ list of reattached fields
947
+ """
948
+ lst = []
949
+ if page is None :
950
+ for p in self .pages :
951
+ lst += self .reattach_fields (p )
952
+ return lst
953
+
954
+ try :
955
+ af = cast (DictionaryObject , self ._root_object [CatalogDictionary .ACRO_FORM ])
956
+ except KeyError :
957
+ af = DictionaryObject ()
958
+ self ._root_object [NameObject (CatalogDictionary .ACRO_FORM )] = af
959
+ try :
960
+ fields = cast (ArrayObject , af [InteractiveFormDictEntries .Fields ])
961
+ except KeyError :
962
+ fields = ArrayObject ()
963
+ af [NameObject (InteractiveFormDictEntries .Fields )] = fields
964
+
965
+ if "/Annots" not in page :
966
+ return lst
967
+ annots = cast (ArrayObject , page ["/Annots" ])
968
+ for idx in range (len (annots )):
969
+ ano = annots [idx ]
970
+ indirect = isinstance (ano , IndirectObject )
971
+ ano = cast (DictionaryObject , ano .get_object ())
972
+ if ano .get ("/Subtype" , "" ) == "/Widget" and "/FT" in ano :
973
+ if ano .indirect_reference in fields :
974
+ continue
975
+ if not indirect :
976
+ annots [idx ] = self ._add_object (ano )
977
+ fields .append (ano .indirect_reference )
978
+ lst .append (ano )
979
+ return lst
980
+
935
981
def clone_reader_document_root (self , reader : PdfReader ) -> None :
936
982
"""
937
983
Copy the reader document root to the writer and all sub elements,
0 commit comments