unipdf/fjson/fjson.go
2023-01-08 22:34:27 +00:00

52 lines
4.4 KiB
Go

//
// 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 (_ag "encoding/json";_bb "github.com/unidoc/unipdf/v3/common";_af "github.com/unidoc/unipdf/v3/core";_ab "github.com/unidoc/unipdf/v3/model";_b "io";_ae "os";);
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_d []fieldValue };
// SetImageFromFile assign image file to a specific field identified by fieldName.
func (_aef *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_cad ,_ec :=_ae .Open (imagePath );if _ec !=nil {return _ec ;};defer _cad .Close ();_ea ,_ec :=_ab .ImageHandling .Read (_cad );if _ec !=nil {_bb .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_ec );
return _ec ;};return _aef .SetImage (fieldName ,_ea ,opt );};
// SetImage assign model.Image to a specific field identified by fieldName.
func (_ge *FieldData )SetImage (fieldName string ,img *_ab .Image ,opt []string )error {_ecg :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_ge ._d =append (_ge ._d ,_ecg );return nil ;};
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _b .Reader )(*FieldData ,error ){var _f FieldData ;_c :=_ag .NewDecoder (r ).Decode (&_f ._d );if _c !=nil {return nil ,_c ;};return &_f ,nil ;};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;ImageValue *_ab .Image `json:"-"`;
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
// FieldValues implements model.FieldValueProvider interface.
func (_ed *FieldData )FieldValues ()(map[string ]_af .PdfObject ,error ){_ad :=make (map[string ]_af .PdfObject );for _ ,_edg :=range _ed ._d {if len (_edg .Value )> 0{_ad [_edg .Name ]=_af .MakeString (_edg .Value );};};return _ad ,nil ;};
// FieldImageValues implements model.FieldImageProvider interface.
func (_gac *FieldData )FieldImageValues ()(map[string ]*_ab .Image ,error ){_gd :=make (map[string ]*_ab .Image );for _ ,_aaa :=range _gac ._d {if _aaa .ImageValue !=nil {_gd [_aaa .Name ]=_aaa .ImageValue ;};};return _gd ,nil ;};
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_aga ,_fb :=_ae .Open (filePath );if _fb !=nil {return nil ,_fb ;};defer _aga .Close ();return LoadFromJSON (_aga );};
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_cac ,_age :=_ae .Open (filePath );if _age !=nil {return nil ,_age ;};defer _cac .Close ();return LoadFromPDF (_cac );};
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _b .ReadSeeker )(*FieldData ,error ){_df ,_aeg :=_ab .NewPdfReader (rs );if _aeg !=nil {return nil ,_aeg ;};if _df .AcroForm ==nil {return nil ,nil ;};var _g []fieldValue ;_cc :=_df .AcroForm .AllFields ();for _ ,_dg :=range _cc {var _ga []string ;
_deb :=make (map[string ]struct{});_cca ,_cd :=_dg .FullName ();if _cd !=nil {return nil ,_cd ;};if _ce ,_gf :=_dg .V .(*_af .PdfObjectString );_gf {_g =append (_g ,fieldValue {Name :_cca ,Value :_ce .Decoded ()});continue ;};var _fa string ;for _ ,_agac :=range _dg .Annotations {_cg ,_dc :=_af .GetName (_agac .AS );
if _dc {_fa =_cg .String ();};_e ,_fbb :=_af .GetDict (_agac .AP );if !_fbb {continue ;};_fc ,_ :=_af .GetDict (_e .Get ("\u004e"));for _ ,_ca :=range _fc .Keys (){_faf :=_ca .String ();if _ ,_dgb :=_deb [_faf ];!_dgb {_ga =append (_ga ,_faf );_deb [_faf ]=struct{}{};
};};_cef ,_ :=_af .GetDict (_e .Get ("\u0044"));for _ ,_afa :=range _cef .Keys (){_ee :=_afa .String ();if _ ,_debd :=_deb [_ee ];!_debd {_ga =append (_ga ,_ee );_deb [_ee ]=struct{}{};};};};_fae :=fieldValue {Name :_cca ,Value :_fa ,Options :_ga };_g =append (_g ,_fae );
};_ege :=FieldData {_d :_g };return &_ege ,nil ;};
// JSON returns the field data as a string in JSON format.
func (_bc FieldData )JSON ()(string ,error ){_dgbd ,_dac :=_ag .MarshalIndent (_bc ._d ,"","\u0020\u0020\u0020\u0020");return string (_dgbd ),_dac ;};