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.
2024-04-16 11:40:43 +00:00
package fjson ;import (_b "encoding/json";_a "github.com/unidoc/unipdf/v3/common";_c "github.com/unidoc/unipdf/v3/core";_ae "github.com/unidoc/unipdf/v3/model";_be "io";_e "os";);
// LoadFromJSON loads JSON form data from `r`.
func LoadFromJSON (r _be .Reader )(*FieldData ,error ){var _d FieldData ;_gb :=_b .NewDecoder (r ).Decode (&_d ._bd );if _gb !=nil {return nil ,_gb ;};return &_d ,nil ;};
// SetImage assign model.Image to a specific field identified by fieldName.
func (_dfg *FieldData )SetImage (fieldName string ,img *_ae .Image ,opt []string )error {_feff :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_dfg ._bd =append (_dfg ._bd ,_feff );return nil ;};
2024-02-11 21:29:32 +00:00
// FieldImageValues implements model.FieldImageProvider interface.
2024-04-16 11:40:43 +00:00
func (_gbd *FieldData )FieldImageValues ()(map[string ]*_ae .Image ,error ){_ec :=make (map[string ]*_ae .Image );for _ ,_fba :=range _gbd ._bd {if _fba .ImageValue !=nil {_ec [_fba .Name ]=_fba .ImageValue ;};};return _ec ,nil ;};
2024-03-27 22:34:33 +00:00
2024-04-16 11:40:43 +00:00
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_bd []fieldValue };type fieldValue struct{Name string `json:"name"`;Value string `json:"value"`;ImageValue *_ae .Image `json:"-"`;
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
2023-10-07 13:58:01 +00:00
2024-01-22 01:16:41 +00:00
// JSON returns the field data as a string in JSON format.
2024-04-16 11:40:43 +00:00
func (_bee FieldData )JSON ()(string ,error ){_afg ,_gdf :=_b .MarshalIndent (_bee ._bd ,"","\u0020\u0020\u0020\u0020");return string (_afg ),_gdf ;};
2023-05-29 17:26:33 +00:00
2024-04-16 11:40:43 +00:00
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _be .ReadSeeker )(*FieldData ,error ){_ag ,_ac :=_ae .NewPdfReader (rs );if _ac !=nil {return nil ,_ac ;};if _ag .AcroForm ==nil {return nil ,nil ;};var _f []fieldValue ;_fd :=_ag .AcroForm .AllFields ();for _ ,_gec :=range _fd {var _cf []string ;
_eg :=make (map[string ]struct{});_egc ,_fa :=_gec .FullName ();if _fa !=nil {return nil ,_fa ;};if _ee ,_aeb :=_gec .V .(*_c .PdfObjectString );_aeb {_f =append (_f ,fieldValue {Name :_egc ,Value :_ee .Decoded ()});continue ;};var _fe string ;for _ ,_fc :=range _gec .Annotations {_gf ,_dg :=_c .GetName (_fc .AS );
if _dg {_fe =_gf .String ();};_gd ,_agg :=_c .GetDict (_fc .AP );if !_agg {continue ;};_cfd ,_ :=_c .GetDict (_gd .Get ("\u004e"));for _ ,_cb :=range _cfd .Keys (){_fef :=_cb .String ();if _ ,_ga :=_eg [_fef ];!_ga {_cf =append (_cf ,_fef );_eg [_fef ]=struct{}{};
};};_gecc ,_ :=_c .GetDict (_gd .Get ("\u0044"));for _ ,_af :=range _gecc .Keys (){_bbg :=_af .String ();if _ ,_aee :=_eg [_bbg ];!_aee {_cf =append (_cf ,_bbg );_eg [_bbg ]=struct{}{};};};};_da :=fieldValue {Name :_egc ,Value :_fe ,Options :_cf };_f =append (_f ,_da );
};_bc :=FieldData {_bd :_f };return &_bc ,nil ;};
2023-12-17 13:54:01 +00:00
2024-04-16 11:40:43 +00:00
// FieldValues implements model.FieldValueProvider interface.
func (_gaa *FieldData )FieldValues ()(map[string ]_c .PdfObject ,error ){_gfc :=make (map[string ]_c .PdfObject );for _ ,_bae :=range _gaa ._bd {if len (_bae .Value )> 0{_gfc [_bae .Name ]=_c .MakeString (_bae .Value );};};return _gfc ,nil ;};
2023-06-30 13:19:48 +00:00
2024-02-11 21:29:32 +00:00
// SetImageFromFile assign image file to a specific field identified by fieldName.
2024-04-16 11:40:43 +00:00
func (_cff *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_ecg ,_afa :=_e .Open (imagePath );if _afa !=nil {return _afa ;};defer _ecg .Close ();_afb ,_afa :=_ae .ImageHandling .Read (_ecg );if _afa !=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",_afa );
return _afa ;};return _cff .SetImage (fieldName ,_afb ,opt );};
2023-08-03 17:30:04 +00:00
2024-03-27 22:34:33 +00:00
// LoadFromJSONFile loads form field data from a JSON file.
2024-04-16 11:40:43 +00:00
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_ba ,_ge :=_e .Open (filePath );if _ge !=nil {return nil ,_ge ;};defer _ba .Close ();return LoadFromJSON (_ba );};
2023-11-11 11:29:03 +00:00
2024-04-16 11:40:43 +00:00
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_aca ,_dfd :=_e .Open (filePath );if _dfd !=nil {return nil ,_dfd ;};defer _aca .Close ();return LoadFromPDF (_aca );};