@@ -821,76 +821,30 @@ class PDFDocument {
821
821
} ) ;
822
822
}
823
823
824
- get xfaDatasets ( ) {
824
+ get xfaStreams ( ) {
825
825
const acroForm = this . catalog . acroForm ;
826
826
if ( ! acroForm ) {
827
- return shadow ( this , "xfaDatasets" , null ) ;
828
- }
829
-
830
- const xfa = acroForm . get ( "XFA" ) ;
831
- if ( xfa instanceof BaseStream && ! xfa . isEmpty ) {
832
- try {
833
- const xdp = stringToUTF8String ( xfa . getString ( ) ) ;
834
- return shadow ( this , "xfaDatasets" , new DatasetReader ( { xdp } ) ) ;
835
- } catch ( _ ) {
836
- warn ( "XFA - Invalid utf-8 string." ) ;
837
- return shadow ( this , "xfaDatasets" , null ) ;
838
- }
839
- }
840
-
841
- if ( ! Array . isArray ( xfa ) || xfa . length === 0 ) {
842
- return null ;
843
- }
844
-
845
- for ( let i = 0 , ii = xfa . length ; i < ii ; i += 2 ) {
846
- if ( xfa [ i ] !== "datasets" ) {
847
- continue ;
848
- }
849
- const data = this . xref . fetchIfRef ( xfa [ i + 1 ] ) ;
850
- if ( ! ( data instanceof BaseStream ) || data . isEmpty ) {
851
- continue ;
852
- }
853
- try {
854
- const datasets = stringToUTF8String ( data . getString ( ) ) ;
855
- return shadow ( this , "xfaDatasets" , new DatasetReader ( { datasets } ) ) ;
856
- } catch ( _ ) {
857
- warn ( "XFA - Invalid utf-8 string." ) ;
858
- return shadow ( this , "xfaDatasets" , null ) ;
859
- }
860
- }
861
-
862
- return shadow ( this , "xfaDatasets" , null ) ;
863
- }
864
-
865
- get xfaData ( ) {
866
- const acroForm = this . catalog . acroForm ;
867
- if ( ! acroForm ) {
868
- return null ;
827
+ return shadow ( this , "xfaStreams" , null ) ;
869
828
}
870
829
871
830
const xfa = acroForm . get ( "XFA" ) ;
872
831
const entries = {
873
- "xdp:xdp" : "" ,
874
- template : "" ,
875
- datasets : "" ,
876
- config : "" ,
877
- connectionSet : "" ,
878
- localeSet : "" ,
879
- stylesheet : "" ,
880
- "/xdp:xdp" : "" ,
832
+ "xdp:xdp" : null ,
833
+ template : null ,
834
+ datasets : null ,
835
+ config : null ,
836
+ connectionSet : null ,
837
+ localeSet : null ,
838
+ stylesheet : null ,
839
+ "/xdp:xdp" : null ,
881
840
} ;
882
841
if ( xfa instanceof BaseStream && ! xfa . isEmpty ) {
883
- try {
884
- entries [ "xdp:xdp" ] = stringToUTF8String ( xfa . getString ( ) ) ;
885
- return entries ;
886
- } catch ( _ ) {
887
- warn ( "XFA - Invalid utf-8 string." ) ;
888
- return null ;
889
- }
842
+ entries [ "xdp:xdp" ] = xfa ;
843
+ return shadow ( this , "xfaStreams" , entries ) ;
890
844
}
891
845
892
846
if ( ! Array . isArray ( xfa ) || xfa . length === 0 ) {
893
- return null ;
847
+ return shadow ( this , "xfaStreams" , null ) ;
894
848
}
895
849
896
850
for ( let i = 0 , ii = xfa . length ; i < ii ; i += 2 ) {
@@ -910,14 +864,52 @@ class PDFDocument {
910
864
if ( ! ( data instanceof BaseStream ) || data . isEmpty ) {
911
865
continue ;
912
866
}
867
+ entries [ name ] = data ;
868
+ }
869
+ return shadow ( this , "xfaStreams" , entries ) ;
870
+ }
871
+
872
+ get xfaDatasets ( ) {
873
+ const streams = this . xfaStreams ;
874
+ if ( ! streams ) {
875
+ return shadow ( this , "xfaDatasets" , null ) ;
876
+ }
877
+ for ( const key of [ "datasets" , "xdp:xdp" ] ) {
878
+ const stream = streams [ key ] ;
879
+ if ( ! stream ) {
880
+ continue ;
881
+ }
882
+ try {
883
+ const str = stringToUTF8String ( stream . getString ( ) ) ;
884
+ const data = Object . fromEntries ( [ [ key , str ] ] ) ;
885
+ return shadow ( this , "xfaDatasets" , new DatasetReader ( data ) ) ;
886
+ } catch ( _ ) {
887
+ warn ( "XFA - Invalid utf-8 string." ) ;
888
+ break ;
889
+ }
890
+ }
891
+ return shadow ( this , "xfaDatasets" , null ) ;
892
+ }
893
+
894
+ get xfaData ( ) {
895
+ const streams = this . xfaStreams ;
896
+ if ( ! streams ) {
897
+ return shadow ( this , "xfaData" , null ) ;
898
+ }
899
+ const data = Object . create ( null ) ;
900
+ for ( const [ key , stream ] of Object . entries ( streams ) ) {
901
+ if ( ! stream ) {
902
+ data [ key ] = "" ;
903
+ continue ;
904
+ }
913
905
try {
914
- entries [ name ] = stringToUTF8String ( data . getString ( ) ) ;
906
+ data [ key ] = stringToUTF8String ( stream . getString ( ) ) ;
915
907
} catch ( _ ) {
916
908
warn ( "XFA - Invalid utf-8 string." ) ;
917
- return null ;
909
+ return shadow ( this , "xfaData" , null ) ;
918
910
}
919
911
}
920
- return entries ;
912
+ return shadow ( this , "xfaData" , data ) ;
921
913
}
922
914
923
915
get xfaFactory ( ) {
0 commit comments