unipdf/fjson/fjson.go
2021-07-30 00:21:16 +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 (_g "encoding/json";_a "github.com/unidoc/unipdf/v3/common";_af "github.com/unidoc/unipdf/v3/core";_df "github.com/unidoc/unipdf/v3/model";_gd "io";_d "os";);
// JSON returns the field data as a string in JSON format.
func (_fcc FieldData )JSON ()(string ,error ){_dfa ,_eb :=_g .MarshalIndent (_fcc ._ab ,"","\u0020\u0020\u0020\u0020");return string (_dfa ),_eb ;};
// FieldImageValues implements model.FieldImageProvider interface.
func (_gcb *FieldData )FieldImageValues ()(map[string ]*_df .Image ,error ){_gca :=make (map[string ]*_df .Image );for _ ,_ae :=range _gcb ._ab {if _ae .ImageValue !=nil {_gca [_ae .Name ]=_ae .ImageValue ;};};return _gca ,nil ;};
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _gd .ReadSeeker )(*FieldData ,error ){_gc ,_f :=_df .NewPdfReader (rs );if _f !=nil {return nil ,_f ;};if _gc .AcroForm ==nil {return nil ,nil ;};var _gfc []fieldValue ;_fg :=_gc .AcroForm .AllFields ();for _ ,_bf :=range _fg {var _fe []string ;
_dg :=make (map[string ]struct{});_gg ,_fc :=_bf .FullName ();if _fc !=nil {return nil ,_fc ;};if _aa ,_gff :=_bf .V .(*_af .PdfObjectString );_gff {_gfc =append (_gfc ,fieldValue {Name :_gg ,Value :_aa .Decoded ()});continue ;};var _dd string ;for _ ,_ag :=range _bf .Annotations {_c ,_fea :=_af .GetName (_ag .AS );
if _fea {_dd =_c .String ();};_ff ,_de :=_af .GetDict (_ag .AP );if !_de {continue ;};_aff ,_ :=_af .GetDict (_ff .Get ("\u004e"));for _ ,_ce :=range _aff .Keys (){_gdf :=_ce .String ();if _ ,_aaf :=_dg [_gdf ];!_aaf {_fe =append (_fe ,_gdf );_dg [_gdf ]=struct{}{};
};};_cc ,_ :=_af .GetDict (_ff .Get ("\u0044"));for _ ,_ggb :=range _cc .Keys (){_bd :=_ggb .String ();if _ ,_afg :=_dg [_bd ];!_afg {_fe =append (_fe ,_bd );_dg [_bd ]=struct{}{};};};};_bfg :=fieldValue {Name :_gg ,Value :_dd ,Options :_fe };_gfc =append (_gfc ,_bfg );
};_dgb :=FieldData {_ab :_gfc };return &_dgb ,nil ;};type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;ImageValue *_df .Image `json:"-"`;
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_gda ,_fa :=_d .Open (filePath );if _fa !=nil {return nil ,_fa ;};defer _gda .Close ();return LoadFromPDF (_gda );};
// SetImageFromFile assign image file to a specific field identified by fieldName.
func (_caf *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_ggf ,_bfe :=_d .Open (imagePath );if _bfe !=nil {return _bfe ;};defer _ggf .Close ();_caa ,_bfe :=_df .ImageHandling .Read (_ggf );if _bfe !=nil {_a .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_bfe );
return _bfe ;};return _caf .SetImage (fieldName ,_caa ,opt );};
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_ab []fieldValue };
// FieldValues implements model.FieldValueProvider interface.
func (_ea *FieldData )FieldValues ()(map[string ]_af .PdfObject ,error ){_cd :=make (map[string ]_af .PdfObject );for _ ,_ca :=range _ea ._ab {if len (_ca .Value )> 0{_cd [_ca .Name ]=_af .MakeString (_ca .Value );};};return _cd ,nil ;};
// SetImage assign model.Image to a specific field identified by fieldName.
func (_ded *FieldData )SetImage (fieldName string ,img *_df .Image ,opt []string )error {_bc :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_ded ._ab =append (_ded ._ab ,_bc );return nil ;};
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_b ,_be :=_d .Open (filePath );if _be !=nil {return nil ,_be ;};defer _b .Close ();return LoadFromJSON (_b );};
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _gd .Reader )(*FieldData ,error ){var _ga FieldData ;_gf :=_g .NewDecoder (r ).Decode (&_ga ._ab );if _gf !=nil {return nil ,_gf ;};return &_ga ,nil ;};