unipdf/fjson/fjson.go

51 lines
4.4 KiB
Go
Raw Normal View History

2020-08-27 21:45:09 +00:00
//
// 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.
2023-09-07 17:40:17 +00:00
package fjson ;import (_e "encoding/json";_eg "github.com/unidoc/unipdf/v3/common";_gg "github.com/unidoc/unipdf/v3/core";_c "github.com/unidoc/unipdf/v3/model";_gf "io";_g "os";);
2022-06-27 19:58:38 +00:00
2023-09-07 17:40:17 +00:00
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_ga []fieldValue };
2023-02-07 17:17:49 +00:00
2023-08-03 17:30:04 +00:00
// SetImageFromFile assign image file to a specific field identified by fieldName.
2023-09-07 17:40:17 +00:00
func (_cdc *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_fbg ,_ca :=_g .Open (imagePath );if _ca !=nil {return _ca ;};defer _fbg .Close ();_gfa ,_ca :=_c .ImageHandling .Read (_fbg );if _ca !=nil {_eg .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_ca );
return _ca ;};return _cdc .SetImage (fieldName ,_gfa ,opt );};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;ImageValue *_c .Image `json:"-"`;
2023-03-01 18:45:57 +00:00
2023-09-07 17:40:17 +00:00
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_fb ,_aa :=_g .Open (filePath );if _aa !=nil {return nil ,_aa ;};defer _fb .Close ();return LoadFromJSON (_fb );};
2023-05-29 17:26:33 +00:00
2023-08-03 17:30:04 +00:00
// LoadFromJSON loads JSON form data from `r`.
2023-09-07 17:40:17 +00:00
func LoadFromJSON (r _gf .Reader )(*FieldData ,error ){var _a FieldData ;_fc :=_e .NewDecoder (r ).Decode (&_a ._ga );if _fc !=nil {return nil ,_fc ;};return &_a ,nil ;};
2023-05-29 17:26:33 +00:00
2023-09-07 17:40:17 +00:00
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _gf .ReadSeeker )(*FieldData ,error ){_ege ,_af :=_c .NewPdfReader (rs );if _af !=nil {return nil ,_af ;};if _ege .AcroForm ==nil {return nil ,nil ;};var _fd []fieldValue ;_ef :=_ege .AcroForm .AllFields ();for _ ,_fg :=range _ef {var _b []string ;
_ec :=make (map[string ]struct{});_gb ,_bd :=_fg .FullName ();if _bd !=nil {return nil ,_bd ;};if _gc ,_cb :=_fg .V .(*_gg .PdfObjectString );_cb {_fd =append (_fd ,fieldValue {Name :_gb ,Value :_gc .Decoded ()});continue ;};var _ggb string ;for _ ,_ba :=range _fg .Annotations {_gaa ,_ff :=_gg .GetName (_ba .AS );
if _ff {_ggb =_gaa .String ();};_efe ,_ge :=_gg .GetDict (_ba .AP );if !_ge {continue ;};_bb ,_ :=_gg .GetDict (_efe .Get ("\u004e"));for _ ,_eb :=range _bb .Keys (){_de :=_eb .String ();if _ ,_bbc :=_ec [_de ];!_bbc {_b =append (_b ,_de );_ec [_de ]=struct{}{};
};};_faa ,_ :=_gg .GetDict (_efe .Get ("\u0044"));for _ ,_bae :=range _faa .Keys (){_ad :=_bae .String ();if _ ,_be :=_ec [_ad ];!_be {_b =append (_b ,_ad );_ec [_ad ]=struct{}{};};};};_deg :=fieldValue {Name :_gb ,Value :_ggb ,Options :_b };_fd =append (_fd ,_deg );
};_fbc :=FieldData {_ga :_fd };return &_fbc ,nil ;};
2023-06-30 13:19:48 +00:00
2023-09-07 17:40:17 +00:00
// JSON returns the field data as a string in JSON format.
func (_bg FieldData )JSON ()(string ,error ){_ggbb ,_cca :=_e .MarshalIndent (_bg ._ga ,"","\u0020\u0020\u0020\u0020");return string (_ggbb ),_cca ;};
2023-07-28 12:14:31 +00:00
2023-09-07 17:40:17 +00:00
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_dec ,_egeb :=_g .Open (filePath );if _egeb !=nil {return nil ,_egeb ;};defer _dec .Close ();return LoadFromPDF (_dec );};
2023-08-03 17:30:04 +00:00
2023-09-07 17:40:17 +00:00
// FieldValues implements model.FieldValueProvider interface.
func (_ebe *FieldData )FieldValues ()(map[string ]_gg .PdfObject ,error ){_cd :=make (map[string ]_gg .PdfObject );for _ ,_geb :=range _ebe ._ga {if len (_geb .Value )> 0{_cd [_geb .Name ]=_gg .MakeString (_geb .Value );};};return _cd ,nil ;};
2023-06-30 13:19:48 +00:00
// FieldImageValues implements model.FieldImageProvider interface.
2023-09-07 17:40:17 +00:00
func (_fce *FieldData )FieldImageValues ()(map[string ]*_c .Image ,error ){_df :=make (map[string ]*_c .Image );for _ ,_eff :=range _fce ._ga {if _eff .ImageValue !=nil {_df [_eff .Name ]=_eff .ImageValue ;};};return _df ,nil ;};
2023-08-03 17:30:04 +00:00
2023-09-07 17:40:17 +00:00
// SetImage assign model.Image to a specific field identified by fieldName.
func (_cg *FieldData )SetImage (fieldName string ,img *_c .Image ,opt []string )error {_ag :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_cg ._ga =append (_cg ._ga ,_ag );return nil ;};