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.
2022-02-05 21:34:53 +00:00
package fjson ;import (_gd "encoding/json";_d "github.com/unidoc/unipdf/v3/common";_af "github.com/unidoc/unipdf/v3/core";_a "github.com/unidoc/unipdf/v3/model";_b "io";_c "os";);
2021-07-30 00:21:16 +00:00
2021-10-22 10:53:20 +00:00
// LoadFromJSON loads JSON form data from `r`.
2022-02-05 21:34:53 +00:00
func LoadFromJSON (r _b .Reader )(*FieldData ,error ){var _f FieldData ;_ff :=_gd .NewDecoder (r ).Decode (&_f ._ag );if _ff !=nil {return nil ,_ff ;};return &_f ,nil ;};
2021-08-13 01:33:42 +00:00
2022-02-05 21:34:53 +00:00
// LoadFromJSONFile loads form field data from a JSON file.
func LoadFromJSONFile (filePath string )(*FieldData ,error ){_e ,_bd :=_c .Open (filePath );if _bd !=nil {return nil ,_bd ;};defer _e .Close ();return LoadFromJSON (_e );};
2021-08-13 01:33:42 +00:00
2022-02-05 21:34:53 +00:00
// FieldValues implements model.FieldValueProvider interface.
func (_fe *FieldData )FieldValues ()(map[string ]_af .PdfObject ,error ){_bfc :=make (map[string ]_af .PdfObject );for _ ,_cg :=range _fe ._ag {if len (_cg .Value )> 0{_bfc [_cg .Name ]=_af .MakeString (_cg .Value );};};return _bfc ,nil ;};type fieldValue struct{Name string `json:"name"`;
Value string `json:"value"`;ImageValue *_a .Image `json:"-"`;
2021-06-21 14:01:56 +00:00
2021-12-14 01:08:28 +00:00
// Options lists allowed values if present.
Options []string `json:"options,omitempty"`;};
2020-10-05 19:28:24 +00:00
2022-02-05 21:34:53 +00:00
// LoadFromPDF loads form field data from a PDF.
func LoadFromPDF (rs _b .ReadSeeker )(*FieldData ,error ){_fa ,_ac :=_a .NewPdfReader (rs );if _ac !=nil {return nil ,_ac ;};if _fa .AcroForm ==nil {return nil ,nil ;};var _afb []fieldValue ;_dd :=_fa .AcroForm .AllFields ();for _ ,_ea :=range _dd {var _bb []string ;
_acd :=make (map[string ]struct{});_ab ,_agf :=_ea .FullName ();if _agf !=nil {return nil ,_agf ;};if _de ,_fb :=_ea .V .(*_af .PdfObjectString );_fb {_afb =append (_afb ,fieldValue {Name :_ab ,Value :_de .Decoded ()});continue ;};var _bf string ;for _ ,_cb :=range _ea .Annotations {_gg ,_dc :=_af .GetName (_cb .AS );
if _dc {_bf =_gg .String ();};_abb ,_eab :=_af .GetDict (_cb .AP );if !_eab {continue ;};_deef ,_ :=_af .GetDict (_abb .Get ("\u004e"));for _ ,_eac :=range _deef .Keys (){_ffg :=_eac .String ();if _ ,_bfa :=_acd [_ffg ];!_bfa {_bb =append (_bb ,_ffg );
_acd [_ffg ]=struct{}{};};};_fg ,_ :=_af .GetDict (_abb .Get ("\u0044"));for _ ,_ba :=range _fg .Keys (){_ae :=_ba .String ();if _ ,_ec :=_acd [_ae ];!_ec {_bb =append (_bb ,_ae );_acd [_ae ]=struct{}{};};};};_fc :=fieldValue {Name :_ab ,Value :_bf ,Options :_bb };
_afb =append (_afb ,_fc );};_aed :=FieldData {_ag :_afb };return &_aed ,nil ;};
2021-07-30 00:21:16 +00:00
2021-12-14 01:08:28 +00:00
// SetImage assign model.Image to a specific field identified by fieldName.
2022-02-05 21:34:53 +00:00
func (_ca *FieldData )SetImage (fieldName string ,img *_a .Image ,opt []string )error {_afc :=fieldValue {Name :fieldName ,ImageValue :img ,Options :opt };_ca ._ag =append (_ca ._ag ,_afc );return nil ;};
2021-10-22 10:53:20 +00:00
2022-02-05 21:34:53 +00:00
// SetImageFromFile assign image file to a specific field identified by fieldName.
func (_eg *FieldData )SetImageFromFile (fieldName string ,imagePath string ,opt []string )error {_bff ,_ege :=_c .Open (imagePath );if _ege !=nil {return _ege ;};defer _bff .Close ();_fda ,_ege :=_a .ImageHandling .Read (_bff );if _ege !=nil {_d .Log .Error ("\u0045\u0072\u0072or\u0020\u006c\u006f\u0061\u0064\u0069\u006e\u0067\u0020\u0069\u006d\u0061\u0067\u0065\u003a\u0020\u0025\u0073",_ege );
return _ege ;};return _eg .SetImage (fieldName ,_fda ,opt );};
2021-10-22 10:53:20 +00:00
2022-02-05 21:34:53 +00:00
// JSON returns the field data as a string in JSON format.
func (_bbf FieldData )JSON ()(string ,error ){_bae ,_ee :=_gd .MarshalIndent (_bbf ._ag ,"","\u0020\u0020\u0020\u0020");return string (_bae ),_ee ;};
2021-12-14 01:08:28 +00:00
2022-02-05 21:34:53 +00:00
// LoadFromPDFFile loads form field data from a PDF file.
func LoadFromPDFFile (filePath string )(*FieldData ,error ){_gda ,_efa :=_c .Open (filePath );if _efa !=nil {return nil ,_efa ;};defer _gda .Close ();return LoadFromPDF (_gda );};
// FieldData represents form field data loaded from JSON file.
type FieldData struct{_ag []fieldValue };
// FieldImageValues implements model.FieldImageProvider interface.
func (_dce *FieldData )FieldImageValues ()(map[string ]*_a .Image ,error ){_eb :=make (map[string ]*_a .Image );for _ ,_ceb :=range _dce ._ag {if _ceb .ImageValue !=nil {_eb [_ceb .Name ]=_ceb .ImageValue ;};};return _eb ,nil ;};