unipdf/fjson/fjson.go

52 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.
2023-05-29 17:26:33 +00:00
package fjson ;import (_c "encoding/json";_db "github.com/unidoc/unipdf/v3/common";_cg "github.com/unidoc/unipdf/v3/core";_ba "github.com/unidoc/unipdf/v3/model";_b "io";_d "os";);
2022-09-10 15:35:04 +00:00
2023-05-29 17:26:33 +00:00
// FieldImageValues implements model.FieldImageProvider interface.
func (_aad *FieldData )FieldImageValues ()(map[string ]*_ba .Image ,error ){_bcf :=make (map[string ]*_ba .Image );for _ ,_ff :=range _aad ._df {if _ff .ImageValue !=nil {_bcf [_ff .Name ]=_ff .ImageValue ;};};return _bcf ,nil ;};
2022-06-27 19:58:38 +00:00
2023-05-29 17:26:33 +00:00
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _b .ReadSeeker )(*FieldData ,error ){_fg ,_bd :=_ba .NewPdfReader (rs );if _bd !=nil {return nil ,_bd ;};if _fg .AcroForm ==nil {return nil ,nil ;};var _a []fieldValue ;_e :=_fg .AcroForm .AllFields ();for _ ,_bbd :=range _e {var _cb []string ;
_ce :=make (map[string ]struct{});_gd ,_ed :=_bbd .FullName ();if _ed !=nil {return nil ,_ed ;};if _de ,_dd :=_bbd .V .(*_cg .PdfObjectString );_dd {_a =append (_a ,fieldValue {Name :_gd ,Value :_de .Decoded ()});continue ;};var _cc string ;for _ ,_fa :=range _bbd .Annotations {_bf ,_bc :=_cg .GetName (_fa .AS );
if _bc {_cc =_bf .String ();};_cbg ,_ccb :=_cg .GetDict (_fa .AP );if !_ccb {continue ;};_ddg ,_ :=_cg .GetDict (_cbg .Get ("\u004e"));for _ ,_aa :=range _ddg .Keys (){_ced :=_aa .String ();if _ ,_ag :=_ce [_ced ];!_ag {_cb =append (_cb ,_ced );_ce [_ced ]=struct{}{};
};};_dcg ,_ :=_cg .GetDict (_cbg .Get ("\u0044"));for _ ,_be :=range _dcg .Keys (){_fb :=_be .String ();if _ ,_bbc :=_ce [_fb ];!_bbc {_cb =append (_cb ,_fb );_ce [_fb ]=struct{}{};};};};_ab :=fieldValue {Name :_gd ,Value :_cc ,Options :_cb };_a =append (_a ,_ab );
};_fc :=FieldData {_df :_a };return &_fc ,nil ;};
2022-09-10 15:35:04 +00:00
2023-03-01 18:45:57 +00:00
// LoadFromJSONFile loads form field data from a JSON file.
2023-05-29 17:26:33 +00:00
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_cd ,_f :=_d .Open (filePath );if _f !=nil {return nil ,_f ;};defer _cd .Close ();return LoadFromJSON (_cd );};
2023-01-08 22:34:27 +00:00
2023-03-01 18:45:57 +00:00
// SetImageFromFile assign image file to a specific field identified by fieldName.
2023-05-29 17:26:33 +00:00
func (_ec *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_bcb ,_baf :=_d .Open (imagePath );if _baf !=nil {return _baf ;};defer _bcb .Close ();_fbd ,_baf :=_ba .ImageHandling .Read (_bcb );if _baf !=nil {_db .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_baf );
return _baf ;};return _ec .SetImage (fieldName ,_fbd ,opt );};
2023-01-08 22:34:27 +00:00
2023-05-29 17:26:33 +00:00
// SetImage assign model.Image to a specific field identified by fieldName.
func (_bfa *FieldData )SetImage (fieldName string ,img *_ba .Image ,opt []string )error {_ae :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_bfa ._df =append (_bfa ._df ,_ae );return nil ;};
2023-01-08 22:34:27 +00:00
2023-04-06 19:57:40 +00:00
// FieldValues implements model.FieldValueProvider interface.
2023-05-29 17:26:33 +00:00
func (_cbb *FieldData )FieldValues ()(map[string ]_cg .PdfObject ,error ){_cf :=make (map[string ]_cg .PdfObject );for _ ,_gf :=range _cbb ._df {if len (_gf .Value )> 0{_cf [_gf .Name ]=_cg .MakeString (_gf .Value );};};return _cf ,nil ;};type fieldValue struct{Name string `json:"name"`;
Value string `json:"value"`;ImageValue *_ba .Image `json:"-"`;
2023-02-07 17:17:49 +00:00
2023-03-01 18:45:57 +00:00
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
2023-05-29 17:26:33 +00:00
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_df []fieldValue };
2023-04-06 19:57:40 +00:00
// LoadFromJSON loads JSON form data from `r`.
2023-05-29 17:26:33 +00:00
func LoadFromJSON (r _b .Reader )(*FieldData ,error ){var _bb FieldData ;_bae :=_c .NewDecoder (r ).Decode (&_bb ._df );if _bae !=nil {return nil ,_bae ;};return &_bb ,nil ;};
// JSON returns the field data as a string in JSON format.
func (_beb FieldData )JSON ()(string ,error ){_ddf ,_eg :=_c .MarshalIndent (_beb ._df ,"","\u0020\u0020\u0020\u0020");return string (_ddf ),_eg ;};
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_edc ,_bbg :=_d .Open (filePath );if _bbg !=nil {return nil ,_bbg ;};defer _edc .Close ();return LoadFromPDF (_edc );};