unipdf/fjson/fjson.go
2022-12-15 21:59:56 +00:00

51 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 (_d "encoding/json";_ba "github.com/unidoc/unipdf/v3/common";_f "github.com/unidoc/unipdf/v3/core";_a "github.com/unidoc/unipdf/v3/model";_g "io";_dg "os";);
// FieldImageValues implements model.FieldImageProvider interface.
func (_cbc *FieldData )FieldImageValues ()(map[string ]*_a .Image ,error ){_ega :=make (map[string ]*_a .Image );for _ ,_ae :=range _cbc ._de {if _ae .ImageValue !=nil {_ega [_ae .Name ]=_ae .ImageValue ;};};return _ega ,nil ;};
// FieldValues implements model.FieldValueProvider interface.
func (_fe *FieldData )FieldValues ()(map[string ]_f .PdfObject ,error ){_ga :=make (map[string ]_f .PdfObject );for _ ,_gf :=range _fe ._de {if len (_gf .Value )> 0{_ga [_gf .Name ]=_f .MakeString (_gf .Value );};};return _ga ,nil ;};
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_ee ,_fb :=_dg .Open (filePath );if _fb !=nil {return nil ,_fb ;};defer _ee .Close ();return LoadFromPDF (_ee );};
// JSON returns the field data as a string in JSON format.
func (_cf FieldData )JSON ()(string ,error ){_gbc ,_cg :=_d .MarshalIndent (_cf ._de ,"","\u0020\u0020\u0020\u0020");return string (_gbc ),_cg ;};
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_aa ,_deg :=_dg .Open (filePath );if _deg !=nil {return nil ,_deg ;};defer _aa .Close ();return LoadFromJSON (_aa );};
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _g .Reader )(*FieldData ,error ){var _ab FieldData ;_db :=_d .NewDecoder (r ).Decode (&_ab ._de );if _db !=nil {return nil ,_db ;};return &_ab ,nil ;};
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _g .ReadSeeker )(*FieldData ,error ){_dgf ,_ad :=_a .NewPdfReader (rs );if _ad !=nil {return nil ,_ad ;};if _dgf .AcroForm ==nil {return nil ,nil ;};var _c []fieldValue ;_bc :=_dgf .AcroForm .AllFields ();for _ ,_fc :=range _bc {var _dc []string ;
_ef :=make (map[string ]struct{});_ca ,_ag :=_fc .FullName ();if _ag !=nil {return nil ,_ag ;};if _bca ,_fcf :=_fc .V .(*_f .PdfObjectString );_fcf {_c =append (_c ,fieldValue {Name :_ca ,Value :_bca .Decoded ()});continue ;};var _gd string ;for _ ,_bb :=range _fc .Annotations {_abd ,_gde :=_f .GetName (_bb .AS );
if _gde {_gd =_abd .String ();};_dge ,_dbc :=_f .GetDict (_bb .AP );if !_dbc {continue ;};_eb ,_ :=_f .GetDict (_dge .Get ("\u004e"));for _ ,_bae :=range _eb .Keys (){_bag :=_bae .String ();if _ ,_da :=_ef [_bag ];!_da {_dc =append (_dc ,_bag );_ef [_bag ]=struct{}{};
};};_cb ,_ :=_f .GetDict (_dge .Get ("\u0044"));for _ ,_eg :=range _cb .Keys (){_df :=_eg .String ();if _ ,_gb :=_ef [_df ];!_gb {_dc =append (_dc ,_df );_ef [_df ]=struct{}{};};};};_ge :=fieldValue {Name :_ca ,Value :_gd ,Options :_dc };_c =append (_c ,_ge );
};_dee :=FieldData {_de :_c };return &_dee ,nil ;};
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_de []fieldValue };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"`;};
// SetImage assign model.Image to a specific field identified by fieldName.
func (_fdde *FieldData )SetImage (fieldName string ,img *_a .Image ,opt []string )error {_feg :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_fdde ._de =append (_fdde ._de ,_feg );return nil ;};
// SetImageFromFile assign image file to a specific field identified by fieldName.
func (_bbd *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_cad ,_bf :=_dg .Open (imagePath );if _bf !=nil {return _bf ;};defer _cad .Close ();_ade ,_bf :=_a .ImageHandling .Read (_cad );if _bf !=nil {_ba .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_bf );
return _bf ;};return _bbd .SetImage (fieldName ,_ade ,opt );};