github.com/unidoc/unipdf/v3@v3.55.0/fjson/fjson.go (about) 1 // 2 // Copyright 2020 FoxyUtils ehf. All rights reserved. 3 // 4 // This is a commercial product and requires a license to operate. 5 // A trial license can be obtained at https://unidoc.io 6 // 7 // DO NOT EDIT: generated by unitwist Go source code obfuscator. 8 // 9 // Use of this source code is governed by the UniDoc End User License Agreement 10 // terms that can be accessed at https://unidoc.io/eula/ 11 12 // Package fjson provides support for loading PDF form field data from JSON data/files. 13 package fjson ;import (_b "encoding/json";_de "github.com/unidoc/unipdf/v3/common";_bf "github.com/unidoc/unipdf/v3/core";_c "github.com/unidoc/unipdf/v3/model";_a "io";_g "os";); 14 15 // FieldData represents form field data loaded from JSON file. 16 type FieldData struct{_da []fieldValue }; 17 18 // FieldImageValues implements model.FieldImageProvider interface. 19 func (_ge *FieldData )FieldImageValues ()(map[string ]*_c .Image ,error ){_abc :=make (map[string ]*_c .Image );for _ ,_afa :=range _ge ._da {if _afa .ImageValue !=nil {_abc [_afa .Name ]=_afa .ImageValue ;};};return _abc ,nil ;}; 20 21 // JSON returns the field data as a string in JSON format. 22 func (_bec FieldData )JSON ()(string ,error ){_gd ,_bee :=_b .MarshalIndent (_bec ._da ,"","\u0020\u0020\u0020\u0020");return string (_gd ),_bee ;}; 23 24 // FieldValues implements model.FieldValueProvider interface. 25 func (_bfg *FieldData )FieldValues ()(map[string ]_bf .PdfObject ,error ){_ccg :=make (map[string ]_bf .PdfObject );for _ ,_deb :=range _bfg ._da {if len (_deb .Value )> 0{_ccg [_deb .Name ]=_bf .MakeString (_deb .Value );};};return _ccg ,nil ;}; 26 27 // LoadFromJSONFile loads form field data from a JSON file. 28 func LoadFromJSONFile (filePath string )(*FieldData ,error ){_gf ,_cf :=_g .Open (filePath );if _cf !=nil {return nil ,_cf ;};defer _gf .Close ();return LoadFromJSON (_gf );}; 29 30 // SetImageFromFile assign image file to a specific field identified by fieldName. 31 func (_eb *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_becd ,_ba :=_g .Open (imagePath );if _ba !=nil {return _ba ;};defer _becd .Close ();_fd ,_ba :=_c .ImageHandling .Read (_becd );if _ba !=nil {_de .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_ba ); 32 return _ba ;};return _eb .SetImage (fieldName ,_fd ,opt );}; 33 34 // SetImage assign model.Image to a specific field identified by fieldName. 35 func (_defc *FieldData )SetImage (fieldName string ,img *_c .Image ,opt []string )error {_ecg :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_defc ._da =append (_defc ._da ,_ecg );return nil ;};type fieldValue struct{Name string `json:"name"`; 36 Value string `json:"value"`;ImageValue *_c .Image `json:"-"`; 37 38 // Options lists allowed values if present. 39 Options []string `json:"options,omitempty"`;}; 40 41 // LoadFromPDFFile loads form field data from a PDF file. 42 func LoadFromPDFFile (filePath string )(*FieldData ,error ){_fbc ,_ab :=_g .Open (filePath );if _ab !=nil {return nil ,_ab ;};defer _fbc .Close ();return LoadFromPDF (_fbc );}; 43 44 // LoadFromPDF loads form field data from a PDF. 45 func LoadFromPDF (rs _a .ReadSeeker )(*FieldData ,error ){_ec ,_bd :=_c .NewPdfReader (rs );if _bd !=nil {return nil ,_bd ;};if _ec .AcroForm ==nil {return nil ,nil ;};var _ca []fieldValue ;_dc :=_ec .AcroForm .AllFields ();for _ ,_ag :=range _dc {var _af []string ; 46 _afc :=make (map[string ]struct{});_bg ,_be :=_ag .FullName ();if _be !=nil {return nil ,_be ;};if _dd ,_dg :=_ag .V .(*_bf .PdfObjectString );_dg {_ca =append (_ca ,fieldValue {Name :_bg ,Value :_dd .Decoded ()});continue ;};var _gc string ;for _ ,_fc :=range _ag .Annotations {_cfa ,_ed :=_bf .GetName (_fc .AS ); 47 if _ed {_gc =_cfa .String ();};_ce ,_aff :=_bf .GetDict (_fc .AP );if !_aff {continue ;};_fa ,_ :=_bf .GetDict (_ce .Get ("\u004e"));for _ ,_fag :=range _fa .Keys (){_bdg :=_fag .String ();if _ ,_gcb :=_afc [_bdg ];!_gcb {_af =append (_af ,_bdg );_afc [_bdg ]=struct{}{}; 48 };};_eee ,_ :=_bf .GetDict (_ce .Get ("\u0044"));for _ ,_ef :=range _eee .Keys (){_cb :=_ef .String ();if _ ,_cc :=_afc [_cb ];!_cc {_af =append (_af ,_cb );_afc [_cb ]=struct{}{};};};};_edd :=fieldValue {Name :_bg ,Value :_gc ,Options :_af };_ca =append (_ca ,_edd ); 49 };_cd :=FieldData {_da :_ca };return &_cd ,nil ;}; 50 51 // LoadFromJSON loads JSON form data from `r`. 52 func LoadFromJSON (r _a .Reader )(*FieldData ,error ){var _e FieldData ;_ee :=_b .NewDecoder (r ).Decode (&_e ._da );if _ee !=nil {return nil ,_ee ;};return &_e ,nil ;};