unipdf/fjson/fjson.go
2022-09-23 18:05:51 +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 (_g "encoding/json";_e "github.com/unidoc/unipdf/v3/common";_a "github.com/unidoc/unipdf/v3/core";_eg "github.com/unidoc/unipdf/v3/model";_c "io";_fc "os";);
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_gcf ,_ec :=_fc .Open (filePath );if _ec !=nil {return nil ,_ec ;};defer _gcf .Close ();return LoadFromPDF (_gcf );};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;
ImageValue *_eg .Image `json:"-"`;
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_ac []fieldValue };
// SetImageFromFile assign image file to a specific field identified by fieldName.
func (_gdag *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_efa ,_ffe :=_fc .Open (imagePath );if _ffe !=nil {return _ffe ;};defer _efa .Close ();_cac ,_ffe :=_eg .ImageHandling .Read (_efa );if _ffe !=nil {_e .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_ffe );
return _ffe ;};return _gdag .SetImage (fieldName ,_cac ,opt );};
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_ab ,_ff :=_fc .Open (filePath );if _ff !=nil {return nil ,_ff ;};defer _ab .Close ();return LoadFromJSON (_ab );};
// SetImage assign model.Image to a specific field identified by fieldName.
func (_fadg *FieldData )SetImage (fieldName string ,img *_eg .Image ,opt []string )error {_gdb :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_fadg ._ac =append (_fadg ._ac ,_gdb );return nil ;};
// FieldValues implements model.FieldValueProvider interface.
func (_ccf *FieldData )FieldValues ()(map[string ]_a .PdfObject ,error ){_gg :=make (map[string ]_a .PdfObject );for _ ,_abf :=range _ccf ._ac {if len (_abf .Value )> 0{_gg [_abf .Name ]=_a .MakeString (_abf .Value );};};return _gg ,nil ;};
// FieldImageValues implements model.FieldImageProvider interface.
func (_fad *FieldData )FieldImageValues ()(map[string ]*_eg .Image ,error ){_fbf :=make (map[string ]*_eg .Image );for _ ,_dee :=range _fad ._ac {if _dee .ImageValue !=nil {_fbf [_dee .Name ]=_dee .ImageValue ;};};return _fbf ,nil ;};
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _c .Reader )(*FieldData ,error ){var _b FieldData ;_bb :=_g .NewDecoder (r ).Decode (&_b ._ac );if _bb !=nil {return nil ,_bb ;};return &_b ,nil ;};
// JSON returns the field data as a string in JSON format.
func (_ca FieldData )JSON ()(string ,error ){_gcg ,_bbc :=_g .MarshalIndent (_ca ._ac ,"","\u0020\u0020\u0020\u0020");return string (_gcg ),_bbc ;};
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _c .ReadSeeker )(*FieldData ,error ){_aa ,_ad :=_eg .NewPdfReader (rs );if _ad !=nil {return nil ,_ad ;};if _aa .AcroForm ==nil {return nil ,nil ;};var _cb []fieldValue ;_ef :=_aa .AcroForm .AllFields ();for _ ,_eb :=range _ef {var _be []string ;
_cd :=make (map[string ]struct{});_df ,_ag :=_eb .FullName ();if _ag !=nil {return nil ,_ag ;};if _agc ,_dg :=_eb .V .(*_a .PdfObjectString );_dg {_cb =append (_cb ,fieldValue {Name :_df ,Value :_agc .Decoded ()});continue ;};var _cc string ;for _ ,_gc :=range _eb .Annotations {_cg ,_cgg :=_a .GetName (_gc .AS );
if _cgg {_cc =_cg .String ();};_gd ,_fb :=_a .GetDict (_gc .AP );if !_fb {continue ;};_ed ,_ :=_a .GetDict (_gd .Get ("\u004e"));for _ ,_bec :=range _ed .Keys (){_beb :=_bec .String ();if _ ,_abe :=_cd [_beb ];!_abe {_be =append (_be ,_beb );_cd [_beb ]=struct{}{};
};};_fge ,_ :=_a .GetDict (_gd .Get ("\u0044"));for _ ,_ebe :=range _fge .Keys (){_gda :=_ebe .String ();if _ ,_fa :=_cd [_gda ];!_fa {_be =append (_be ,_gda );_cd [_gda ]=struct{}{};};};};_ga :=fieldValue {Name :_df ,Value :_cc ,Options :_be };_cb =append (_cb ,_ga );
};_efd :=FieldData {_ac :_cb };return &_efd ,nil ;};