@@ -72,6 +72,11 @@ import {
7272 PdfPageGeometry ,
7373 PdfRun ,
7474 toIntRect ,
75+ PdfDocumentJavaScriptActionObject ,
76+ PdfWidgetJavaScriptActionObject ,
77+ PdfJavaScriptActionTrigger ,
78+ PdfJavaScriptWidgetEventType ,
79+ PDF_ANNOT_AACTION_EVENT ,
7580 Quad ,
7681 PdfAnnotationState ,
7782 PdfAnnotationStateModel ,
@@ -826,6 +831,64 @@ export class PdfiumNative implements IPdfiumExecutor {
826831 return task ;
827832 }
828833
834+ getDocumentJavaScriptActions (
835+ doc : PdfDocumentObject ,
836+ ) : PdfTask < PdfDocumentJavaScriptActionObject [ ] , PdfErrorReason > {
837+ this . logger . debug ( LOG_SOURCE , LOG_CATEGORY , 'getDocumentJavaScriptActions' , doc ) ;
838+
839+ const ctx = this . cache . getContext ( doc . id ) ;
840+ if ( ! ctx ) {
841+ return PdfTaskHelper . reject ( {
842+ code : PdfErrorCode . DocNotOpen ,
843+ message : 'document does not open' ,
844+ } ) ;
845+ }
846+
847+ const count = this . pdfiumModule . FPDFDoc_GetJavaScriptActionCount ( ctx . docPtr ) ;
848+ if ( count < 0 ) {
849+ return PdfTaskHelper . reject ( {
850+ code : PdfErrorCode . Unknown ,
851+ message : 'failed to read document javascript actions' ,
852+ } ) ;
853+ }
854+
855+ const actions : PdfDocumentJavaScriptActionObject [ ] = [ ] ;
856+ for ( let index = 0 ; index < count ; index ++ ) {
857+ const actionPtr = this . pdfiumModule . FPDFDoc_GetJavaScriptAction ( ctx . docPtr , index ) ;
858+ if ( ! actionPtr ) continue ;
859+
860+ try {
861+ const name =
862+ readString (
863+ this . pdfiumModule . pdfium ,
864+ ( buffer : number , bufferLength ) =>
865+ this . pdfiumModule . FPDFJavaScriptAction_GetName ( actionPtr , buffer , bufferLength ) ,
866+ this . pdfiumModule . pdfium . UTF16ToString ,
867+ ) ?? '' ;
868+ const script =
869+ readString (
870+ this . pdfiumModule . pdfium ,
871+ ( buffer : number , bufferLength ) =>
872+ this . pdfiumModule . FPDFJavaScriptAction_GetScript ( actionPtr , buffer , bufferLength ) ,
873+ this . pdfiumModule . pdfium . UTF16ToString ,
874+ ) ?? '' ;
875+
876+ if ( ! script ) continue ;
877+
878+ actions . push ( {
879+ id : `document:${ index } :${ name } ` ,
880+ trigger : PdfJavaScriptActionTrigger . DocumentNamed ,
881+ name,
882+ script,
883+ } ) ;
884+ } finally {
885+ this . pdfiumModule . FPDFDoc_CloseJavaScriptAction ( actionPtr ) ;
886+ }
887+ }
888+
889+ return PdfTaskHelper . resolve ( actions ) ;
890+ }
891+
829892 getPageAnnoWidgets (
830893 doc : PdfDocumentObject ,
831894 page : PdfPageObject ,
@@ -876,6 +939,108 @@ export class PdfiumNative implements IPdfiumExecutor {
876939 return PdfTaskHelper . resolve ( annotationWidgets ) ;
877940 }
878941
942+ getPageWidgetJavaScriptActions (
943+ doc : PdfDocumentObject ,
944+ page : PdfPageObject ,
945+ ) : PdfTask < PdfWidgetJavaScriptActionObject [ ] , PdfErrorReason > {
946+ this . logger . debug ( LOG_SOURCE , LOG_CATEGORY , 'getPageWidgetJavaScriptActions' , doc , page ) ;
947+
948+ const ctx = this . cache . getContext ( doc . id ) ;
949+ if ( ! ctx ) {
950+ return PdfTaskHelper . reject ( {
951+ code : PdfErrorCode . DocNotOpen ,
952+ message : 'document does not open' ,
953+ } ) ;
954+ }
955+
956+ const actions : PdfWidgetJavaScriptActionObject [ ] = [ ] ;
957+ ctx . borrowPage ( page . index , ( pageCtx ) => {
958+ pageCtx . withFormHandle ( ( formHandle ) => {
959+ const annotCount = this . pdfiumModule . FPDFPage_GetAnnotCount ( pageCtx . pagePtr ) ;
960+ for ( let i = 0 ; i < annotCount ; i ++ ) {
961+ pageCtx . withAnnotation ( i , ( annotPtr ) => {
962+ const subtype = this . pdfiumModule . FPDFAnnot_GetSubtype (
963+ annotPtr ,
964+ ) as PdfAnnotationObject [ 'type' ] ;
965+ if ( subtype !== PdfAnnotationSubtype . WIDGET ) return ;
966+
967+ let annotationId = this . getAnnotString ( annotPtr , 'NM' ) ;
968+ if ( ! annotationId || ! isUuidV4 ( annotationId ) ) {
969+ annotationId = uuidV4 ( ) ;
970+ this . setAnnotString ( annotPtr , 'NM' , annotationId ) ;
971+ }
972+
973+ const fieldName =
974+ readString (
975+ this . pdfiumModule . pdfium ,
976+ ( buffer : number , bufferLength ) =>
977+ this . pdfiumModule . FPDFAnnot_GetFormFieldName (
978+ formHandle ,
979+ annotPtr ,
980+ buffer ,
981+ bufferLength ,
982+ ) ,
983+ this . pdfiumModule . pdfium . UTF16ToString ,
984+ ) ?? '' ;
985+
986+ const eventConfigs = [
987+ {
988+ event : PDF_ANNOT_AACTION_EVENT . KEY_STROKE ,
989+ eventType : PdfJavaScriptWidgetEventType . Keystroke ,
990+ trigger : PdfJavaScriptActionTrigger . WidgetKeystroke ,
991+ } ,
992+ {
993+ event : PDF_ANNOT_AACTION_EVENT . FORMAT ,
994+ eventType : PdfJavaScriptWidgetEventType . Format ,
995+ trigger : PdfJavaScriptActionTrigger . WidgetFormat ,
996+ } ,
997+ {
998+ event : PDF_ANNOT_AACTION_EVENT . VALIDATE ,
999+ eventType : PdfJavaScriptWidgetEventType . Validate ,
1000+ trigger : PdfJavaScriptActionTrigger . WidgetValidate ,
1001+ } ,
1002+ {
1003+ event : PDF_ANNOT_AACTION_EVENT . CALCULATE ,
1004+ eventType : PdfJavaScriptWidgetEventType . Calculate ,
1005+ trigger : PdfJavaScriptActionTrigger . WidgetCalculate ,
1006+ } ,
1007+ ] as const ;
1008+
1009+ for ( const config of eventConfigs ) {
1010+ const script =
1011+ readString (
1012+ this . pdfiumModule . pdfium ,
1013+ ( buffer : number , bufferLength ) =>
1014+ this . pdfiumModule . FPDFAnnot_GetFormAdditionalActionJavaScript (
1015+ formHandle ,
1016+ annotPtr ,
1017+ config . event ,
1018+ buffer ,
1019+ bufferLength ,
1020+ ) ,
1021+ this . pdfiumModule . pdfium . UTF16ToString ,
1022+ ) ?? '' ;
1023+
1024+ if ( ! script ) continue ;
1025+
1026+ actions . push ( {
1027+ id : `widget:${ page . index } :${ annotationId } :${ config . eventType } ` ,
1028+ trigger : config . trigger ,
1029+ eventType : config . eventType ,
1030+ pageIndex : page . index ,
1031+ annotationId,
1032+ fieldName,
1033+ script,
1034+ } ) ;
1035+ }
1036+ } ) ;
1037+ }
1038+ } ) ;
1039+ } ) ;
1040+
1041+ return PdfTaskHelper . resolve ( actions ) ;
1042+ }
1043+
8791044 regenerateWidgetAppearances (
8801045 doc : PdfDocumentObject ,
8811046 page : PdfPageObject ,
@@ -2135,7 +2300,13 @@ export class PdfiumNative implements IPdfiumExecutor {
21352300 }
21362301
21372302 this . pdfiumModule . FORM_ForceToKillFocus ( formHandle ) ;
2138- this . pdfiumModule . EPDFAnnot_GenerateFormFieldAP ( annotationPtr ) ;
2303+
2304+ if (
2305+ field . type !== PDF_FORM_FIELD_TYPE . CHECKBOX &&
2306+ field . type !== PDF_FORM_FIELD_TYPE . RADIOBUTTON
2307+ ) {
2308+ this . pdfiumModule . EPDFAnnot_GenerateFormFieldAP ( annotationPtr ) ;
2309+ }
21392310
21402311 this . logger . perf (
21412312 LOG_SOURCE ,
0 commit comments