// // Copyright 2020 FoxyUtils ehf. All rights reserved. // // This is a commercial product and requires a license to operate. // A trial license can be obtained at https://unidoc.io // // DO NOT EDIT: generated by unitwist Go source code obfuscator. // // Use of this source code is governed by the UniDoc End User License Agreement // terms that can be accessed at https://unidoc.io/eula/ // Package fjson provides support for loading PDF form field data from JSON data/files. package fjson ;import (_gd "encoding/json";_d "github.com/unidoc/unipdf/v3/common";_af "github.com/unidoc/unipdf/v3/core";_a "github.com/unidoc/unipdf/v3/model";_b "io";_c "os";); // LoadFromJSON loads JSON form data from `r`. func LoadFromJSON (r _b .Reader )(*FieldData ,error ){var _f FieldData ;_ff :=_gd .NewDecoder (r ).Decode (&_f ._ag );if _ff !=nil {return nil ,_ff ;};return &_f ,nil ;}; // LoadFromJSONFile loads form field data from a JSON file. func LoadFromJSONFile (filePath string )(*FieldData ,error ){_e ,_bd :=_c .Open (filePath );if _bd !=nil {return nil ,_bd ;};defer _e .Close ();return LoadFromJSON (_e );}; // FieldValues implements model.FieldValueProvider interface. func (_fe *FieldData )FieldValues ()(map[string ]_af .PdfObject ,error ){_bfc :=make (map[string ]_af .PdfObject );for _ ,_cg :=range _fe ._ag {if len (_cg .Value )> 0{_bfc [_cg .Name ]=_af .MakeString (_cg .Value );};};return _bfc ,nil ;};type fieldValue struct{Name string `json:"name"`; Value string `json:"value"`;ImageValue *_a .Image `json:"-"`; // Options lists allowed values if present. Options []string `json:"options,omitempty"`;}; // LoadFromPDF loads form field data from a PDF. func LoadFromPDF (rs _b .ReadSeeker )(*FieldData ,error ){_fa ,_ac :=_a .NewPdfReader (rs );if _ac !=nil {return nil ,_ac ;};if _fa .AcroForm ==nil {return nil ,nil ;};var _afb []fieldValue ;_dd :=_fa .AcroForm .AllFields ();for _ ,_ea :=range _dd {var _bb []string ; _acd :=make (map[string ]struct{});_ab ,_agf :=_ea .FullName ();if _agf !=nil {return nil ,_agf ;};if _de ,_fb :=_ea .V .(*_af .PdfObjectString );_fb {_afb =append (_afb ,fieldValue {Name :_ab ,Value :_de .Decoded ()});continue ;};var _bf string ;for _ ,_cb :=range _ea .Annotations {_gg ,_dc :=_af .GetName (_cb .AS ); if _dc {_bf =_gg .String ();};_abb ,_eab :=_af .GetDict (_cb .AP );if !_eab {continue ;};_deef ,_ :=_af .GetDict (_abb .Get ("\u004e"));for _ ,_eac :=range _deef .Keys (){_ffg :=_eac .String ();if _ ,_bfa :=_acd [_ffg ];!_bfa {_bb =append (_bb ,_ffg ); _acd [_ffg ]=struct{}{};};};_fg ,_ :=_af .GetDict (_abb .Get ("\u0044"));for _ ,_ba :=range _fg .Keys (){_ae :=_ba .String ();if _ ,_ec :=_acd [_ae ];!_ec {_bb =append (_bb ,_ae );_acd [_ae ]=struct{}{};};};};_fc :=fieldValue {Name :_ab ,Value :_bf ,Options :_bb }; _afb =append (_afb ,_fc );};_aed :=FieldData {_ag :_afb };return &_aed ,nil ;}; // SetImage assign model.Image to a specific field identified by fieldName. func (_ca *FieldData )SetImage (fieldName string ,img *_a .Image ,opt []string )error {_afc :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_ca ._ag =append (_ca ._ag ,_afc );return nil ;}; // SetImageFromFile assign image file to a specific field identified by fieldName. func (_eg *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_bff ,_ege :=_c .Open (imagePath );if _ege !=nil {return _ege ;};defer _bff .Close ();_fda ,_ege :=_a .ImageHandling .Read (_bff );if _ege !=nil {_d .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_ege ); return _ege ;};return _eg .SetImage (fieldName ,_fda ,opt );}; // JSON returns the field data as a string in JSON format. func (_bbf FieldData )JSON ()(string ,error ){_bae ,_ee :=_gd .MarshalIndent (_bbf ._ag ,"","\u0020\u0020\u0020\u0020");return string (_bae ),_ee ;}; // LoadFromPDFFile loads form field data from a PDF file. func LoadFromPDFFile (filePath string )(*FieldData ,error ){_gda ,_efa :=_c .Open (filePath );if _efa !=nil {return nil ,_efa ;};defer _gda .Close ();return LoadFromPDF (_gda );}; // FieldData represents form field data loaded from JSON file. type FieldData struct{_ag []fieldValue }; // FieldImageValues implements model.FieldImageProvider interface. func (_dce *FieldData )FieldImageValues ()(map[string ]*_a .Image ,error ){_eb :=make (map[string ]*_a .Image );for _ ,_ceb :=range _dce ._ag {if _ceb .ImageValue !=nil {_eb [_ceb .Name ]=_ceb .ImageValue ;};};return _eb ,nil ;};